-
-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix compression and tests therefore #108
Conversation
@@ -54,7 +54,7 @@ object Requester{ | |||
} | |||
case class Requester(verb: String, | |||
sess: BaseSession){ | |||
|
|||
val CMD = verb.toUpperCase // allow submitting as lower case ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you adding CMD
as a field here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"verb" was basically used as an enum, only without insisting it be case dependent. In general it was accessed using by comparing "POST" or something to verb.toUpperCase. One place in the code it was accessed without uppercasing, which I took to be a bug. Maybe I'm old-school, but I don't like code that does the same thing over and over even if the compiler will probably optimize it away, so I changed it to do the upper-casing once and compare to CMD. Maybe there is a style you prefer, but I would fix that bug in any case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some first comments.
for((k, v) <- blobHeaders) connection.setRequestProperty(k, v) | ||
|
||
for((k, v) <- sess.headers) connection.setRequestProperty(k, v) | ||
|
||
for((k, v) <- headers) connection.setRequestProperty(k, v) | ||
|
||
for((k, v) <- compress.headers) connection.setRequestProperty(k, v) | ||
Seq(blobHeaders, sess.headers, headers, compress.headers). | ||
foreach(h => for((k, v) <- h) connection.setRequestProperty(k, v)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This creates an extra Seq
that the old code was avoiding.
I would rollback it, also doesn't help fixing the bug you found.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, just a thought
val bytes = new ByteArrayOutputStream() | ||
withOs(compress.wrap(bytes)) { os => data.write(os) } | ||
val byteArray = bytes.toByteArray | ||
connection.setFixedLengthStreamingMode(byteArray.length) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting seems weird here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I see that, but suit yourself.
/** | ||
* Do something with an OutputStream and close it | ||
* @param os OutputStream | ||
* @param fn | ||
*/ | ||
private def withOs[T](os: OutputStream)(fn: OutputStream => T) : Unit = | ||
try fn(os) finally os.close() | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Scaladoc here doesn't add much value.
Also I suggest you to rename the method to usingOutputStream
to be consistent with Scala 2.13's Using
/** | |
* Do something with an OutputStream and close it | |
* @param os OutputStream | |
* @param fn | |
*/ | |
private def withOs[T](os: OutputStream)(fn: OutputStream => T) : Unit = | |
try fn(os) finally os.close() | |
private def usingOutputStream[T](os: OutputStream)(fn: OutputStream => T) : Unit = | |
try fn(os) finally os.close() | |
test("send"){ | ||
requests.send("get")("https://httpbin.org/get?hello=world&foo=baz") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happened with formatting here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The formatting complaints are more obvious in the editor. Yeah, I can twiddle them.
import java.net.InetSocketAddress | ||
|
||
import com.sun.net.httpserver.HttpExchange | ||
import com.sun.net.httpserver.HttpHandler | ||
import com.sun.net.httpserver.HttpServer | ||
import java.io._ | ||
import java.util.zip._ | ||
import scala.collection.mutable.StringBuilder | ||
import utest._ | ||
import ujson._ | ||
import requests.Compress | ||
import requests.Compress._ | ||
import scala.annotation.tailrec |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you organize these imports here?
// } | ||
// } | ||
|
||
//Tests fail with 'Request to https://httpbin.org/absolute-redirect/4 failed with status code 404' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you fix those tests? If so you should remove this comment, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd buy that
"https://httpbin.org/post", | ||
compress = requests.Compress.None, | ||
data = new RequestBlob.ByteSourceRequestBlob("Hello World") | ||
) | ||
assert(res1.text.contains(""""Hello World"""")) | ||
|
||
val res2 = requests.post( | ||
"https://httpbin.org/post", | ||
compress = requests.Compress.Gzip, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting is weird here
/** | ||
* Compress with each compression mode and call server. Server expands | ||
* and passes it back so we can compare | ||
*/ | ||
test("compressionData") { | ||
import Compress._ | ||
val str = "I am deflater mouse" | ||
Seq(None, Gzip, Deflate).foreach(c => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting is weird here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you could be slightly more specific on what is weird where you don't like the formatting. One of then looks a little over-indented, but I wouldn't know what to change on the others. I've never done this before, and in my imagination you were going to merge in the parts you found agreeable and hit format;-)
import com.sun.net.httpserver.HttpExchange | ||
import com.sun.net.httpserver.HttpHandler | ||
import com.sun.net.httpserver.HttpServer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work on Java 11+ ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought I tested for all versions. I can try again ...
Hi @tballard, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
Sorry for the long turnaround!
Good job on the fix and on setting up a server for testing!
I changed the code to close some output streams so the server can actually get all the data. I added some tests using a local server so it could catch the compressed data, decompress it and send it back to be checked against the original.
Also the code was allowing the verb to be passed in as any case and then checking it by uppercasing and comparing whenever it was used. Except that somebody forgot to upper case it in one place which seemed like a bug. I changed the code to upper case it in one spot.
There were 4 small blocks of code that were trivially reduced to 1 and who could resist?
There were some existing compression tests that were failing once the change was made. The test was to make sure the response data had a specific value. One got the impression that someone just wrote the test after seeing the returned value, but who knows? I changed the tests to match the new returned values.
I uncommented some tests and they didn't blow up, so I left them. Maybe I went rogue ...
I don't think there is anything else of substance.