Skip to content

Commit 992ddec

Browse files
author
Justin Dahmubed
committed
Cleanup and add unit tests
1 parent ab8634d commit 992ddec

9 files changed

Lines changed: 156 additions & 10 deletions

File tree

src/org/oidc/common/HttpMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
* The Http method used to send the request
55
*/
66
public enum HttpMethod {
7-
POST, PUT, GET;
7+
POST, GET;
88
}

src/org/oidc/service/AbstractService.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public Message parseResponse(String responseBody, SerializationType serializatio
184184
}
185185

186186
if(SerializationType.URL_ENCODED.equals(serializationType)) {
187-
responseBody = ServiceUtil.getUrlInfo(responseBody);
187+
responseBody = ServiceUtil.getUrlQueryReference(responseBody);
188188
}
189189

190190
Message response = null;
@@ -222,7 +222,7 @@ public Message parseResponse(String responseBody, SerializationType serializatio
222222
throw new OidcServiceError("Verification of the response failed");
223223
}
224224

225-
if(response.getType() instanceof AuthorizationResponse && Strings.isNullOrEmpty(response.getScope())) {
225+
if(response instanceof AuthorizationResponse && Strings.isNullOrEmpty(response.getScope())) {
226226
response.setScope(addedClaims.getScope());
227227
}
228228

@@ -281,6 +281,10 @@ public Message parseResponse(
281281
* @return HttpArguments
282282
*/
283283
public HttpArguments getRequestParameters(Map<String,String> requestArguments) {
284+
if(requestArguments == null) {
285+
throw new IllegalArgumentException("null requestArguments");
286+
}
287+
284288
if(Strings.isNullOrEmpty(requestArguments.get(HTTP_METHOD))) {
285289
requestArguments.put(HTTP_METHOD, this.httpMethod.name());
286290
}
@@ -293,6 +297,8 @@ public HttpArguments getRequestParameters(Map<String,String> requestArguments) {
293297
requestArguments.put(SERIALIZATION_TYPE, this.serializationType.name());
294298
}
295299

300+
//request = self.construct_request(request_args=request_args, **kwargs)
301+
296302
HttpArguments httpArguments = new HttpArguments();
297303
httpArguments.setHttpMethod(httpMethod);
298304

src/org/oidc/service/base/HttpArguments.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class HttpArguments {
99

1010
/**
11-
* Specifies whether it is a POST, PUT or GET request
11+
* Specifies whether it is a POST or GET request
1212
*/
1313
private HttpMethod httpMethod;
1414
/**

src/org/oidc/service/base/ServiceConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ServiceConfig {
2121
*/
2222
private ClientAuthenticationMethod defaultAuthenticationMethod;
2323
/**
24-
* The HTTP method (POST, PUT, GET) that are to be used to transmit the
24+
* The HTTP method (POST, GET) that are to be used to transmit the
2525
* request.
2626
*/
2727
private HttpMethod httpMethod;

src/org/oidc/service/base/ServiceContext.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,15 @@ public class ServiceContext {
110110

111111
public ServiceContext(
112112
KeyJar keyJar,
113-
ServiceContextConfig config)
114-
throws NoSuchFieldException, IllegalAccessException {
113+
ServiceContextConfig config) {
115114
this.keyJar = keyJar;
116115
this.config = config;
117116
}
118117

118+
public ServiceContext() {
119+
120+
}
121+
119122
/**
120123
The client needs its own set of keys. It can either dynamically create them or load them from local storage. This method can also fetch other entities keys provided that the URL points to a JWKS.
121124
* @param keySpecifications contains fileName and algorithm
@@ -204,5 +207,17 @@ public String getIssuer() {
204207
public KeyJar getKeyJar() {
205208
return keyJar;
206209
}
210+
211+
public void setIssuer(String issuer) {
212+
this.issuer = issuer;
213+
}
214+
215+
public ServiceContextConfig getConfig() {
216+
return config;
217+
}
218+
219+
public Map<String, Boolean> getAllow() {
220+
return allow;
221+
}
207222
}
208223

src/org/oidc/service/base/ServiceContextConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
public class ServiceContextConfig {
1818

19-
2019
/**
2120
* Where dynamically received or statically assigned provider
2221
* information is stored

src/org/oidc/service/util/ServiceUtil.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ public class ServiceUtil {
1616
* @param url a URL possibly containing a query or a fragment part
1717
* @return the query/reference part
1818
**/
19-
public static String getUrlInfo(String url) throws MalformedURLException {
19+
public static String getUrlQueryReference(String url) throws MalformedURLException {
20+
if(Strings.isNullOrEmpty(url)) {
21+
throw new IllegalArgumentException("null or empty url");
22+
}
2023
String queryOrReference = null;
21-
if(!Strings.isNullOrEmpty(url) && url.contains("?") && url.contains("#")) {
24+
if(url.contains("?") && url.contains("#")) {
2225
URL urlObject = new URL(url);
2326
String query = urlObject.getQuery();
2427
String reference = urlObject.getRef();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.oidc.service.base;
2+
3+
import com.auth0.msg.KeyJar;
4+
import org.junit.Assert;
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.junit.rules.ExpectedException;
8+
import org.oidc.common.ValueError;
9+
10+
public class ServiceContextTest {
11+
12+
@Rule
13+
public ExpectedException thrown = ExpectedException.none();
14+
15+
@Test
16+
public void testFileNameFromWebnameNullUrl() throws Exception{
17+
thrown.expect(IllegalArgumentException.class);
18+
thrown.expectMessage("null or empty webName");
19+
ServiceContext serviceContext = new ServiceContext();
20+
serviceContext.fileNameFromWebname(null);
21+
}
22+
23+
@Test
24+
public void testFileNameFromWebnameEmptyUrl() throws Exception{
25+
thrown.expect(IllegalArgumentException.class);
26+
thrown.expectMessage("null or empty webName");
27+
ServiceContext serviceContext = new ServiceContext();
28+
serviceContext.fileNameFromWebname("");
29+
}
30+
31+
@Test
32+
public void testFileNameFromWebnameWhereWebNameDoesntStartWithBaseUrl() throws Exception {
33+
thrown.expect(ValueError.class);
34+
thrown.expectMessage("Webname does not match baseUrl");
35+
ServiceContextConfig serviceContextConfig = new ServiceContextConfig.ServiceContextConfigBuilder().setBaseUrl("baseUrl")
36+
.buildServiceContext();
37+
KeyJar keyJar = new KeyJar();
38+
ServiceContext serviceContext = new ServiceContext(keyJar, serviceContextConfig);
39+
serviceContext.fileNameFromWebname("webName");
40+
}
41+
42+
@Test
43+
public void testFileNameFromWebnameWhereWebNameStartsWithForwardSlash() throws Exception {
44+
ServiceContextConfig serviceContextConfig = new ServiceContextConfig.ServiceContextConfigBuilder().setBaseUrl("www.yahoo.com")
45+
.buildServiceContext();
46+
KeyJar keyJar = new KeyJar();
47+
ServiceContext serviceContext = new ServiceContext(keyJar, serviceContextConfig);
48+
String fileName = serviceContext.fileNameFromWebname("www.yahoo.com/1234");
49+
Assert.assertTrue(fileName.equals("1234"));
50+
}
51+
52+
@Test
53+
public void testFileNameFromWebnameWhereWebNameDoesntStartsWithForwardSlash() throws Exception {
54+
ServiceContextConfig serviceContextConfig = new ServiceContextConfig.ServiceContextConfigBuilder().setBaseUrl("www.yahoo.com")
55+
.buildServiceContext();
56+
KeyJar keyJar = new KeyJar();
57+
ServiceContext serviceContext = new ServiceContext(keyJar, serviceContextConfig);
58+
String fileName = serviceContext.fileNameFromWebname("www.yahoo.com:1234");
59+
Assert.assertTrue(fileName.equals(":1234"));
60+
}
61+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.oidc.service.util;
2+
3+
import static org.hamcrest.core.StringContains.containsString;
4+
5+
import java.net.MalformedURLException;
6+
import org.junit.Assert;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.rules.ExpectedException;
10+
import org.oidc.common.SerializationType;
11+
import org.oidc.common.UnsupportedContentType;
12+
13+
public class ServiceUtilTest {
14+
15+
@Rule
16+
public ExpectedException thrown = ExpectedException.none();
17+
18+
@Test
19+
public void testGetQueryReferenceNullUrl() throws Exception{
20+
thrown.expect(MalformedURLException.class);
21+
thrown.expectMessage("null or empty url");
22+
ServiceUtil.getUrlQueryReference(null);
23+
}
24+
25+
@Test
26+
public void testGetQueryReferenceEmptyUrl() throws Exception{
27+
thrown.expect(MalformedURLException.class);
28+
thrown.expectMessage("null or empty url");
29+
ServiceUtil.getUrlQueryReference("");
30+
}
31+
32+
@Test
33+
public void testGetUrlQueryReferenceWithQueryExcluded() throws Exception{
34+
String url = ServiceUtil.getUrlQueryReference("https://www.google.co.in/?gfe_rd=cr&ei=ptYq" +
35+
"WK26I4fT8gfth6CACg#q=geeks+for+geeks+java");
36+
Assert.assertTrue(url.equals("q=gnu&rlz=1C1CHZL_enIN714IN715&oq=gnu&aqs=chrome..69i57j69i60l5.653j0j7&" +
37+
"sourceid=chrome&ie=UTF-8"));
38+
}
39+
40+
@Test
41+
public void testGetUrlQueryReferenceWithQueryIncluded() throws Exception{
42+
String url = ServiceUtil.getUrlQueryReference("https://www.google.co.in/#q=geeks+for+geeks+java");
43+
Assert.assertTrue(url.equals("q=geeks+for+geeks+java"));
44+
}
45+
46+
@Test
47+
public void testGetHttpBodyWithSerializationTypeUrlEncoded() {
48+
ServiceUtil.getHttpBody(new AuthorizationResponse(), SerializationType.URL_ENCODED);
49+
}
50+
51+
@Test
52+
public void testGetHttpBodyWithSerializationTypeJson() {
53+
ServiceUtil.getHttpBody(new AuthorizationResponse(), SerializationType.JSON);
54+
}
55+
56+
@Test
57+
public void testGetHttpBodyWithIncorrectSerializationType() {
58+
thrown.expect(UnsupportedContentType.class);
59+
thrown.expectMessage(containsString("Unsupported content type: "));
60+
ServiceUtil.getHttpBody(new AuthorizationResponse(), SerializationType.JWT);
61+
}
62+
}

0 commit comments

Comments
 (0)