Skip to content

Commit e37b1f7

Browse files
committed
Automatically populate default CookieManager so returned session cookies are automatically added on subsequent HTTP Requests
1 parent 1b54cd9 commit e37b1f7

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/AndroidClient/client/src/main/java/net/servicestack/client/JsonServiceClient.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import com.google.gson.Gson;
77
import com.google.gson.GsonBuilder;
8-
import com.google.gson.JsonArray;
98
import com.google.gson.JsonElement;
109
import com.google.gson.JsonObject;
1110

@@ -16,15 +15,14 @@
1615
import java.io.InputStreamReader;
1716
import java.io.UnsupportedEncodingException;
1817
import java.lang.reflect.Field;
19-
import java.lang.reflect.Modifier;
2018
import java.lang.reflect.Type;
19+
import java.net.CookieHandler;
20+
import java.net.CookieManager;
2121
import java.net.HttpURLConnection;
2222
import java.net.URL;
2323
import java.net.URLEncoder;
2424
import java.nio.charset.Charset;
25-
import java.util.ArrayList;
2625
import java.util.Date;
27-
import java.util.Iterator;
2826
import java.util.Map;
2927
import java.util.UUID;
3028

@@ -50,6 +48,11 @@ public class JsonServiceClient implements ServiceClient {
5048
public JsonServiceClient(String baseUrl) {
5149
this.baseUrl = baseUrl.endsWith("/") ? baseUrl : baseUrl + "/";
5250
this.replyUrl = this.baseUrl + "json/reply/";
51+
52+
//Automatically populate response cookies
53+
if (CookieHandler.getDefault() == null){
54+
CookieHandler.setDefault(new CookieManager());
55+
}
5356
}
5457

5558
public void setTimeout(int timeoutMs) {

src/AndroidClient/client/src/test/java/net/servicestack/client/tests/TestAuthTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,24 @@ public void test_does_transparently_send_BasicAuthHeader_on_401_response(){
5252
assertEquals("test DisplayName", response.getDisplayName());
5353
assertNotNull(response.getSessionId());
5454
}
55+
56+
public void test_can_authenticate_with_CredentialsAuth(){
57+
ServiceClient client = CreateClient();
58+
59+
AuthenticateResponse authResponse = client.post(new Authenticate()
60+
.setProvider("credentials")
61+
.setUserName("test")
62+
.setPassword("test"));
63+
64+
assertEquals("1", authResponse.getUserId());
65+
assertEquals("test", authResponse.getUserName());
66+
assertNotNull(authResponse.getSessionId());
67+
68+
TestAuthResponse response = client.get(new TestAuth());
69+
70+
assertEquals("1", response.getUserId());
71+
assertEquals("test", response.getUserName());
72+
assertEquals("test DisplayName", response.getDisplayName());
73+
assertNotNull(response.getSessionId());
74+
}
5575
}

0 commit comments

Comments
 (0)