akheon23 / mockwebserver Goto Github PK
View Code? Open in Web Editor NEWAutomatically exported from code.google.com/p/mockwebserver
License: Apache License 2.0
Automatically exported from code.google.com/p/mockwebserver
License: Apache License 2.0
Having trouble getting to the bottom of why my test fails with
SocketTimeoutException unless I remove Expect 100-continue header. Any ideas?
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:152)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:677)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:623)
at sun.net.www.protocol.http.HttpURLConnection.expect100Continue(HttpURLConnection.java:1020)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1105)
Code:
public void testExpect100() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setBody("hello world"));
server.play();
URL url = server.getUrl("/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(1000);
connection.setReadTimeout(1000);
connection.setAllowUserInteraction(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Expect", "100-continue"); // << comment this and it will pass
connection.setRequestProperty(CONTENT_LENGTH, "4");
connection.setFixedLengthStreamingMode(4);
connection.setDoOutput(true);
connection.getOutputStream().write(new byte [] {1, 2, 3, 4});
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
assertEquals("hello world", reader.readLine());
RecordedRequest request = server.takeRequest();
assertEquals("POST / HTTP/1.1", request.getRequestLine());
}
Original issue reported on code.google.com by [email protected]
on 1 Mar 2013 at 9:15
looks great. I'd like to try it in my test executions, but would need this
thrown into maven for that to work. Any chance we can nudge this into maven
central?
Original issue reported on code.google.com by [email protected]
on 2 Jan 2012 at 11:07
QueueDispatcher returns canned 404 responses for /favicon.ico requests, but
MockWebServer records these requests and exposes them via takeRequest().
Ideally they would be filtered at both ends.
Original issue reported on code.google.com by [email protected]
on 20 Nov 2013 at 5:50
Should use a Logger instead.
Original issue reported on code.google.com by [email protected]
on 20 Nov 2013 at 5:52
HATEOAS apis send back link headers or links embedded in xml or json. It is
sometimes the case that a client expects to resolve these as absolute. It
would be nice to have a way to either defer expansion of a
MockWebServerResponse or somehow else allow templating.
lacking means to do this, I hacked a little and do a lazy replace of the word
"URL" in a QueueDispatcher.
/**
* there's no built-in way to defer evaluation of a response body, hence this
* method, which allows us to send back links to the mock server.
*/
private AtomicReference<URL> setURLReplacingDispatcher(MockWebServer server) {
final AtomicReference<URL> url = new AtomicReference<URL>();
final QueueDispatcher dispatcher = new QueueDispatcher() {
protected final BlockingQueue<MockResponse> responseQueue = new LinkedBlockingQueue<MockResponse>();
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
MockResponse response = responseQueue.take();
if (response.getBody() != null) {
String newBody = new String(response.getBody()).replace("URL", url.get().toString());
response = response.setBody(newBody);
}
return response;
}
@Override
public void enqueueResponse(MockResponse response) {
responseQueue.add(response);
}
};
server.setDispatcher(dispatcher);
return url;
}
Original issue reported on code.google.com by [email protected]
on 25 Mar 2013 at 4:31
Hello,
we face a very rare situation : in one of our app, we need to embed the
mockwebserver library inside the app itself, not in its tests. Our client wants
an app that can be used in a "demo mode" that will return mocks of webservices
(static json files mostly). Thus we embedded the mockwebserver inside the app
to replay the mocks.
But we are now facing a problem : the mockwebserver can only be tested via the
network : by sending real requests and checking if the results match expected
json responses.
Nevertheless, it would be possible, by some minor changes in the architecture,
to make the mockwebserver testable directly without using the network.
For us, only one of them would be interesting : being able to test a
dispatcher. To achieve this, it would be enough to have 1) a public constructor
for RecordedRequest or 2) make the class non final.
Would it be possible for the mockwebserver team to provide us with a public
constructor inside RecordedRequest so that we can create custom Requests,
inject them in the dispatcher and test if our dispatcher returns the
appropriate response ?
That's really an urgent need for us. If the project would have been on github
we would have been pleased to submit a pull request for this. Without this
change, we will have to fork mockwebserver and we would prefer not too as your
lib is really great and we would like to keep it in sync with your great work.
Thanks in advance,
Stéphane
Original issue reported on code.google.com by [email protected]
on 25 Jan 2013 at 6:27
spring android (and I believe spring rest template not only for android ) adds
a content length header to ANY request. Even GET methods will have a body set.
And this is actually not compatible with mock web server. I really agree that
this is a bad practice they adopted but, that the way their code is :
http://grepcode.com/file/repo1.maven.org/maven2/org.springframework.android/spri
ng-android-rest-template/1.0.0.RELEASE/org/springframework/http/client/SimpleBuf
feringClientHttpRequest.java#SimpleBufferingClientHttpRequest
Their code is really complex (bloated ?) and will be hard to change. On
Android, requests can be of 2 kinds : one for pre-FROYO and one for post-FROYO.
Moreover the maintainer of the lib doesn't seem to go deep in the core of the
lib anymore.
In our current app, this makes our tests fail. It simply crashes the mock
server we use and all subsequent tests will fail. We tried hard to hack spring
android to change this, but it's not possible.
Would you mind to add a parameter so that mockwebserver is less conservative
and accepts this kind of requests...
I am not used with SVN at all, I use git/github usually. And I don't know how
to submit a patch to you about this. So, I hope you will forgive this but I
will submit a working patch and 2 tests for it attached to this issue.
** Actually, if you could release this to maven central, it would tremendously
help our project. **
Let me know if there is anything I coud do to help with it.
Thanks in advance, and really, mock web server is a great piece of software,
damn useful for testing real apps. Thx for writing and maintaining it.
:)
Stéphane
PS : I am sorry, I lost your formatting when I wrote the patch, but there are
around 100 characters that changed + tests.
Original issue reported on code.google.com by [email protected]
on 21 May 2013 at 7:46
Attachments:
The following test case fails on Java 7:
@Test
public void emptyPutHangs() throws Exception {
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().addHeader("ETag", "ABCDEF"));
server.play();
URL url = server.getUrl("/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(0);
//connection.setReadTimeout(1000); // << comment out the previous line and uncomment this and it will pass
connection.setAllowUserInteraction(false);
connection.setRequestMethod("PUT");
connection.setRequestProperty("Expect", "100-continue"); // << comment this and it will pass
connection.setRequestProperty("Content-Length", "0");
connection.setDoOutput(true);
connection.setFixedLengthStreamingMode(0);
//connection.setFixedLengthStreamingMode(1); // comment out the previous line and uncomment these two and it will pass
//connection.getOutputStream().write(new byte [] { 1 });
assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
RecordedRequest request = server.takeRequest();
assertEquals(request.getRequestLine(), "PUT / HTTP/1.1");
server.shutdown();
}
Removing the Expect 100-Continue header or writing a non-empty body allows the
test to succeed, as does setting a non-zero read timeout (in that case, the
read simply times out).
The read timeout is set to 0 because of issue 6
Original issue reported on code.google.com by [email protected]
on 12 Apr 2013 at 12:44
Attachments:
Pull Jeff Sharkey's change in from here:
https://android-review.googlesource.com/#/c/52192/
Original issue reported on code.google.com by limpbizkit
on 25 Feb 2013 at 6:07
In MockWebServer.readRequest when using an ssl socket factory, the following
code block can swallow an SSLHandshakeException which makes debugging more
difficult. I think that should be instead propagated or at least logged.
} catch (IOException streamIsClosed) {
return null; // no request because we closed the stream
}
Original issue reported on code.google.com by [email protected]
on 25 Jun 2013 at 3:40
What steps will reproduce the problem?
1. Set up mockwebserver and enqueue a request
server.enqueue(new MockResponse(...));
server.play();
2. Create a jersey-client and make a post without a body:
client.resource(server.getURL("/")).path("/").post(MyResponseType.class)
What is the expected output? What do you see instead?
I get an IllegalArgumentException from MockWebServer.java, readRequest()
method.
What version of the product are you using? On what operating system?
The only public release.
Please provide any additional information below.
MockWebServer checks for a non-empty POST body and fails if empty. Should this
be the right behaviour? Curl seems to allow POST's without body (or provide
empty body?). I'm bringing curl here because I have to implement an API that
allows this:
curl -X POST -u admin:admin \
'http://localhost:7180/api/v1/clusters/Cluster%201%20-%20CDH4/services/my_hbase/commands/hbaseCreateRoot'
Original issue reported on code.google.com by [email protected]
on 10 Aug 2012 at 12:12
Hello,
I'm looking at this project and it's almost perfect for my needs! One thing
it's missing for me is a way to get a count of the number of socket connections
that have been made to the mock webserver since it was started.
It's a long story, but the reason I need this is that I have implemented an
HTTP 1.1 client that needs to act as if HTTP keep alive has been disabled. I
need to make sure that every request uses a new socket rather than reusing
connections, so the only way I can think to verify this is happening would be
to use your project and then be able to compare the number of requests I have
made to the server vs how many connections the server has seen. Does that make
sense?
I would think about submitting a patch myself but it looks like it would maybe
take quite a bit of refactoring to make that work...
Original issue reported on code.google.com by [email protected]
on 28 Jun 2012 at 5:45
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.