use of com.okta.oidc.net.ConnectionParameters in project okta-oidc-android by okta.
the class ConnectionParametersTest method create.
@Test
public void create() throws UnsupportedEncodingException {
ConnectionParameters parameters = new ParameterBuilder().setRequestMethod(RequestMethod.POST).setRequestType(RequestType.AUTHORIZE).setPostParameter("post1", "post1").setRequestProperty("prop1", "prop1").setPostParameters(Collections.singletonMap("post2", "post2")).setRequestProperties(Collections.singletonMap("prop2", "prop2")).create();
mConnParams = new ParameterBuilder().setRequestMethod(RequestMethod.POST).setRequestType(RequestType.AUTHORIZE).setPostParameter("post1", "post1").setRequestProperty("prop1", "prop1").setPostParameters(Collections.singletonMap("post2", "post2")).setRequestProperties(Collections.singletonMap("prop2", "prop2")).create();
String decode1 = new String(parameters.getEncodedPostParameters(), DEFAULT_ENCODING);
String decode2 = new String(mConnParams.getEncodedPostParameters(), DEFAULT_ENCODING);
assertEquals(decode1, decode2);
assertEquals(parameters.getRequestType(), mConnParams.getRequestType());
assertEquals(parameters.postParameters(), mConnParams.postParameters());
assertEquals(parameters.requestProperties(), mConnParams.requestProperties());
assertEquals(parameters.requestMethod(), mConnParams.requestMethod());
}
use of com.okta.oidc.net.ConnectionParameters in project okta-oidc-android by okta.
the class OkHttp method buildRequest.
protected Request buildRequest(Uri uri, ConnectionParameters param) {
try {
mTrustAllSslContext = SSLContext.getInstance("SSL");
mTrustAllSslContext.init(null, mTrustManager, new java.security.SecureRandom());
} catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new RuntimeException(e);
}
mTrustAllSslSocketFactory = mTrustAllSslContext.getSocketFactory();
if (sOkHttpClient == null) {
sOkHttpClient = new OkHttpClient.Builder().readTimeout(param.readTimeOutMs(), TimeUnit.MILLISECONDS).connectTimeout(param.connectionTimeoutMs(), TimeUnit.MILLISECONDS).hostnameVerifier((hostname, session) -> true).sslSocketFactory(mTrustAllSslSocketFactory, (X509TrustManager) mTrustManager[0]).build();
}
Request.Builder requestBuilder = new Request.Builder().url(uri.toString());
for (Map.Entry<String, String> headerEntry : param.requestProperties().entrySet()) {
String key = headerEntry.getKey();
requestBuilder.addHeader(key, headerEntry.getValue());
}
if (param.requestMethod() == ConnectionParameters.RequestMethod.GET) {
requestBuilder = requestBuilder.get();
} else {
Map<String, String> postParameters = param.postParameters();
if (postParameters != null) {
FormBody.Builder formBuilder = new FormBody.Builder();
for (Map.Entry<String, String> postEntry : postParameters.entrySet()) {
String key = postEntry.getKey();
formBuilder.add(key, postEntry.getValue());
}
RequestBody formBody = formBuilder.build();
requestBuilder.post(formBody);
} else {
requestBuilder.post(RequestBody.create(null, ""));
}
}
return requestBuilder.build();
}
use of com.okta.oidc.net.ConnectionParameters in project okta-oidc-android by okta.
the class ConnectionParametersTest method testBuilderWithTimeOut.
@Test
public void testBuilderWithTimeOut() throws Exception {
mServer.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
URL url = mServer.url("/").url();
Date start = DateUtil.getNow();
ConnectionParameters parameters = new ParameterBuilder().setRequestMethod(RequestMethod.GET).create();
HttpClientImpl httpClient = new HttpClientImpl(5000, 2000);
httpClient.connect(Uri.parse(url.toString()), parameters);
try {
httpClient.getResponseCode();
} catch (SocketTimeoutException ex) {
// NO-OP
}
long end = DateUtil.getNow().getTime();
Date date = new Date(end - 2000);
assertEquals(start.toString(), date.toString());
}
use of com.okta.oidc.net.ConnectionParameters in project okta-oidc-android by okta.
the class ConnectionParametersTest method testBuilderWithRequestProperty.
@Test
public void testBuilderWithRequestProperty() throws Exception {
mServer.enqueue(new MockResponse());
Map<String, String> properties = new HashMap<>();
properties.put(USER_AGENT, CUSTOM_USER_AGENT);
properties.put("PROP", "PROP");
URL url = mServer.url("/").url();
ConnectionParameters parameters = new ParameterBuilder().setRequestMethod(RequestMethod.GET).setRequestProperties(properties).setRequestProperty("PROP1", "PROP1").create();
HttpClientImpl httpClient = new HttpClientImpl();
httpClient.connect(Uri.parse(url.toString()), parameters);
int code = httpClient.getResponseCode();
RecordedRequest recordedRequest = mServer.takeRequest();
assertEquals(code, HTTP_OK);
// ConnectionParameters overrides custom user agent.
assertEquals(recordedRequest.getHeader(USER_AGENT), USER_AGENT_HEADER);
assertEquals(recordedRequest.getHeader("PROP"), "PROP");
assertEquals(recordedRequest.getHeader("PROP1"), "PROP1");
}
use of com.okta.oidc.net.ConnectionParameters in project okta-oidc-android by okta.
the class ConnectionParametersTest method testBuilderWithPostParameters.
@Test
public void testBuilderWithPostParameters() throws Exception {
mServer.enqueue(new MockResponse());
Map<String, String> params = new HashMap<>();
params.put("client_id", CLIENT_ID);
params.put("grant_type", GrantTypes.AUTHORIZATION_CODE);
params.put("redirect_uri", REDIRECT_URI);
params.put("code_verifier", "code_verifier");
params.put("code", "code");
params.put("nonce", "nonce");
URL url = mServer.url("/").url();
ConnectionParameters parameters = new ParameterBuilder().setRequestMethod(RequestMethod.POST).setPostParameters(params).setPostParameter("POST", "POST").create();
HttpClientImpl httpClient = new HttpClientImpl();
httpClient.connect(Uri.parse(url.toString()), parameters);
int code = httpClient.getResponseCode();
RecordedRequest recordedRequest = mServer.takeRequest();
String postbody = recordedRequest.getBody().readUtf8Line();
assertEquals(code, HTTP_OK);
Assert.assertTrue(postbody.contains("client_id=" + CLIENT_ID));
Assert.assertTrue(postbody.contains("grant_type=" + GrantTypes.AUTHORIZATION_CODE));
Assert.assertTrue(postbody.contains("redirect_uri=" + URLEncoder.encode(REDIRECT_URI, "UTF-8")));
Assert.assertTrue(postbody.contains("code_verifier=code_verifier"));
Assert.assertTrue(postbody.contains("code=code"));
Assert.assertTrue(postbody.contains("nonce=nonce"));
}
Aggregations