Skip to content

Commit 91297ee

Browse files
authored
Merge pull request #40 from auth0/add-authorize-url-builder-methods
Add response_type and custom parameter setter for AuthorizeUrlBuilder
2 parents 6e5a73b + 24d9aef commit 91297ee

2 files changed

Lines changed: 69 additions & 4 deletions

File tree

src/main/java/com/auth0/client/auth/AuthorizeUrlBuilder.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ private AuthorizeUrlBuilder(String domain, String clientId, String redirectUri)
3838
builder = HttpUrl.parse(domain).newBuilder()
3939
.addPathSegment("authorize")
4040
.addEncodedQueryParameter("redirect_uri", redirectUri)
41-
.addQueryParameter("response_type", "code")
4241
.addQueryParameter("client_id", clientId);
42+
withParameter("response_type", "code");
4343
}
4444

4545
/**
@@ -90,6 +90,32 @@ public AuthorizeUrlBuilder withScope(String scope) {
9090
return this;
9191
}
9292

93+
/**
94+
* Sets the response type value.
95+
*
96+
* @param responseType response type to set
97+
* @return the builder instance
98+
*/
99+
public AuthorizeUrlBuilder withResponseType(String responseType) {
100+
assertNotNull(responseType, "response type");
101+
parameters.put("response_type", responseType);
102+
return this;
103+
}
104+
105+
/**
106+
* Sets an additional parameter.
107+
*
108+
* @param name name of the parameter
109+
* @param value value of the parameter to set
110+
* @return the builder instance
111+
*/
112+
public AuthorizeUrlBuilder withParameter(String name, String value) {
113+
assertNotNull(name, "name");
114+
assertNotNull(value, "value");
115+
parameters.put(name, value);
116+
return this;
117+
}
118+
93119
/**
94120
* Creates a string representation of the URL with the configured parameters.
95121
*

src/test/java/com/auth0/client/auth/AuthorizeUrlBuilderTest.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,50 @@ public void shouldSetScope() throws Exception {
156156
}
157157

158158
@Test
159-
public void shouldThrowWithStateIsNull() throws Exception {
159+
public void shouldThrowWhenScopeIsNull() throws Exception {
160160
exception.expect(IllegalArgumentException.class);
161-
exception.expectMessage("'state' cannot be null!");
161+
exception.expectMessage("'scope' cannot be null!");
162162
AuthorizeUrlBuilder.newInstance(DOMAIN, CLIENT_ID, REDIRECT_URI)
163-
.withState(null);
163+
.withScope(null);
164+
}
165+
166+
@Test
167+
public void shouldSetResponseType() throws Exception {
168+
String url = AuthorizeUrlBuilder.newInstance(DOMAIN, CLIENT_ID, REDIRECT_URI)
169+
.withResponseType("token id_token")
170+
.build();
171+
assertThat(url, hasQueryParameter("response_type", "token id_token"));
172+
}
173+
174+
@Test
175+
public void shouldThrowWhenResponseTypeIsNull() throws Exception {
176+
exception.expect(IllegalArgumentException.class);
177+
exception.expectMessage("'response type' cannot be null!");
178+
AuthorizeUrlBuilder.newInstance(DOMAIN, CLIENT_ID, REDIRECT_URI)
179+
.withResponseType(null);
180+
}
181+
182+
@Test
183+
public void shouldSetCustomParameter() throws Exception {
184+
String url = AuthorizeUrlBuilder.newInstance(DOMAIN, CLIENT_ID, REDIRECT_URI)
185+
.withParameter("name", "value")
186+
.build();
187+
assertThat(url, hasQueryParameter("name", "value"));
164188
}
165189

190+
@Test
191+
public void shouldThrowWhenCustomParameterNameIsNull() throws Exception {
192+
exception.expect(IllegalArgumentException.class);
193+
exception.expectMessage("'name' cannot be null!");
194+
AuthorizeUrlBuilder.newInstance(DOMAIN, CLIENT_ID, REDIRECT_URI)
195+
.withParameter(null, "value");
196+
}
197+
198+
@Test
199+
public void shouldThrowWhenCustomParameterValueIsNull() throws Exception {
200+
exception.expect(IllegalArgumentException.class);
201+
exception.expectMessage("'value' cannot be null!");
202+
AuthorizeUrlBuilder.newInstance(DOMAIN, CLIENT_ID, REDIRECT_URI)
203+
.withParameter("name", null);
204+
}
166205
}

0 commit comments

Comments
 (0)