0

I tried to set cookies in my Android Instrumentationtest like this:

  @Override
protected MockResponse onDispatch(RecordedRequest request) {
    if (request.getPath().startsWith("/?loginkulcs=")) {
        MockResponse response = new MockResponse()
                .setStatus(new BasicStatusLine(new ProtocolVersion("HTTP",1,1), 302, "Moved Temporarily").toString())
                .setHeaders(new Headers.Builder()
                        .add("Date", LocalDateTime.now().toString("ddd, dd MMM yyyy HH:mm:ss Z"))
                        .add("Server", "GlassFish Server Open Source Edition 3.1.2.2")
                        .add("X-Powered-By", "Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.2.2 Java/Oracle Corporation/1.7)")
                        .add("Location","https://xxx.jsp")
                        .add("Content-Type","text/html;charset=ISO-8859-1")
                        .add("Content-Language","en-US")
                        .add("Content-Length","227")
                        .add("Keep-Alive","timeout=5, max=100")
                        .add("Connection", "Keep-Alive")
                        .add("Set-Cookie", String.format("session_id=%s; Path=/xxx_teszt/", SESSION_ID))
                        .add("Set-Cookie", "regisztralt=IGEN; Path=/xxx_teszt/")
                        .add("Set-Cookie", "session_id=2084e74283d5073c5f4d324f17e2; Path=/xxx_teszt; HttpOnly")
                        .build());
        return response;
    }
    return null;

}

the android client is an org.apache.http.impl.client.DefaultHttpClient and it reads cookies like this:

client.getCookieStore().getCookies();

But the result is null. How can I set the cookies for this client?

thx

Zamek

zamek 42
  • 793
  • 1
  • 9
  • 15

1 Answers1

0

it is Cookie and not Set-Cookie; see the MockWebServerTest, for example:

.addHeader("Cookie: " + String.format("session_id=%s; Path=/xxx_teszt/", SESSION_ID))
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Sorry the original is working like this, there is a sessionId by a cookie, when the I get an 302 code. I don't know does it conform with standard or doesn't, but I have to do it. – zamek 42 Aug 20 '18 at 15:57
  • @zamekz you'd have to fix the headers on the server-side, and then fix the mock of it. the page after the `302`, which then sends `200` should set the cookie headers. or just run one test, which tests the `302` redirect and another one, which tests the redirect destination, which should send a `200`. – Martin Zeitler Aug 20 '18 at 16:07
  • I tried to set return code 200 instead of 302, but the cookies are empty:( – zamek 42 Aug 21 '18 at 15:58
  • @zamekz it it actually setting any headers? – Martin Zeitler Aug 21 '18 at 17:22
  • I use only http. It is working in a live webserver, I just want to create a test case for this, because I cannot generate test data on the live webserver. The live webserver working with these data, I simply copy the header data from a live data. The live webserver sends three different cookies, I think so cookies should be a map which contains list as a value. – zamek 42 Aug 23 '18 at 07:19
  • @zamekz just have found the mistake (which I've suspected) and updated my answer... please accept, because the URL of the tests for the mock-webserver, should reliably answer the question. – Martin Zeitler Aug 23 '18 at 07:40
  • I checked the stored values after I create it, and everything seems to be good, except that, every key converted to small letter: key:cookie value:regisztralt=IGEN; Path=/enaplo_teszt/ value:session_id=32a53dc792a605b87d6ea75415be8e48; Path=/enaplo_teszt/ – zamek 42 Aug 23 '18 at 08:57
  • @zamekz does the same happen with the live web-server? it's not really clear if cookie names are indeed case-sensitive, whether or not... there are 3 RFC, which obsolete each other. some state they are, other say, that aren't. here in this case it only matters how the cookies of the live web-server are being handled. – Martin Zeitler Aug 23 '18 at 09:05
  • Sorry it was my mistake, because I wanted to print all headers after I added to response. I could get it with toMultiMap() which is convert all header keys to lowercase. But the sent headers was correct, not converted. – zamek 42 Aug 24 '18 at 08:25
  • I found the problem! The client rejected the cookie because: "illegal path attribute" Path of origin"/ message. In test I don't need to use these Path property. I removed it and it works well. Thank you for your help. – zamek 42 Aug 27 '18 at 08:34