Skip to content

Commit 1e559b7

Browse files
fix(auth-query-params): include auth query params in request (#96)
This commit fixes the problem of not including auth query parameters in the API request. Now request builder makes sure to include the query params specific to auth.
1 parent a60a6a3 commit 1e559b7

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

src/main/java/io/apimatic/core/HttpRequest.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.util.AbstractMap.SimpleEntry;
55
import java.util.ArrayList;
6+
import java.util.Collections;
67
import java.util.HashMap;
78
import java.util.HashSet;
89
import java.util.List;
@@ -79,16 +80,24 @@ private HttpRequest(final GlobalConfiguration coreConfig, final String server,
7980
final ArraySerializationFormat arraySerializationFormat) throws IOException {
8081
this.coreConfig = coreConfig;
8182
this.compatibilityFactory = coreConfig.getCompatibilityFactory();
82-
queryUrlBuilder = getStringBuilder(server, path, queryParams, arraySerializationFormat);
83+
HttpHeaders requestHeaders = addHeaders(headerParams);
84+
// Creating a basic request to provide it to auth instances
85+
Request request = buildBasicRequest(httpMethod, requestHeaders);
86+
87+
applyAuthentication(request, authentication);
88+
// include auth query parameters in request query params to have them in the query url
89+
if (request.getQueryParameters() != null) {
90+
queryParams.putAll(request.getQueryParameters());
91+
}
8392

93+
queryUrlBuilder = getStringBuilder(server, path, queryParams, arraySerializationFormat);
8494
processTemplateParams(templateParams);
8595
Object bodyValue = buildBody(body, bodySerializer, bodyParameters);
8696
List<SimpleEntry<String, Object>> formFields =
8797
generateFormFields(formParams, formParameters, arraySerializationFormat);
8898
coreHttpRequest =
89-
buildRequest(httpMethod, bodyValue, addHeaders(headerParams), queryParams,
99+
buildRequest(httpMethod, bodyValue, requestHeaders, queryParams,
90100
formFields, arraySerializationFormat);
91-
applyAuthentication(authentication);
92101
}
93102

94103
/**
@@ -111,14 +120,26 @@ private Request buildRequest(
111120
queryParams, formFields);
112121
}
113122

114-
private void applyAuthentication(Authentication authentication) {
123+
/**
124+
* Builds a request with minimal query parameters.
125+
* @param httpMethod The request HTTP verb.
126+
* @param headerParams The request header parameters.
127+
* @return the {@link Request} instance.
128+
*/
129+
private Request buildBasicRequest(Method httpMethod, HttpHeaders headerParams)
130+
throws IOException {
131+
return compatibilityFactory.createHttpRequest(httpMethod, null, headerParams,
132+
new HashMap<String, Object>(), Collections.emptyList());
133+
}
134+
135+
private void applyAuthentication(Request request, Authentication authentication) {
115136
if (authentication != null) {
116137
authentication.validate();
117138
if (!authentication.isValid()) {
118139
throw new AuthValidationException(authentication.getErrorMessage());
119140
}
120141

121-
authentication.apply(coreHttpRequest);
142+
authentication.apply(request);
122143
}
123144
}
124145

src/test/java/apimatic/core/EndToEndTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.mockito.ArgumentMatchers.any;
66
import static org.mockito.ArgumentMatchers.anyList;
77
import static org.mockito.ArgumentMatchers.anyMap;
8+
import static org.mockito.ArgumentMatchers.nullable;
89
import static org.mockito.Mockito.when;
910

1011
import java.io.IOException;
@@ -519,6 +520,9 @@ private void prepareStub() throws IOException {
519520
when(getCompatibilityFactory().createHttpRequest(any(Method.class),
520521
any(StringBuilder.class), any(HttpHeaders.class), anyMap(), anyList()))
521522
.thenReturn(coreHttpRequest);
523+
when(getCompatibilityFactory().createHttpRequest(any(Method.class),
524+
nullable(StringBuilder.class), nullable(HttpHeaders.class), anyMap(), anyList()))
525+
.thenReturn(coreHttpRequest);
522526
when(getCompatibilityFactory().createHttpContext(coreHttpRequest, response))
523527
.thenReturn(context);
524528
when(context.getResponse()).thenReturn(response);

src/test/java/apimatic/core/RequestBuilderTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static org.mockito.Mockito.when;
88
import static org.mockito.Mockito.doAnswer;
99
import static org.mockito.Mockito.any;
10+
import static org.mockito.Mockito.nullable;
1011
import static org.mockito.Mockito.anyString;
1112
import static org.mockito.Mockito.anyMap;
1213
import static org.mockito.Mockito.anyList;
@@ -868,6 +869,10 @@ private void prepareCompatibilityStub() {
868869
when(getCompatibilityFactory().createHttpRequest(any(Method.class),
869870
any(StringBuilder.class), any(HttpHeaders.class), anyMap(), anyList()))
870871
.thenReturn(getCoreHttpRequest());
872+
873+
when(getCompatibilityFactory().createHttpRequest(any(Method.class),
874+
nullable(StringBuilder.class), nullable(HttpHeaders.class), anyMap(), anyList()))
875+
.thenReturn(getCoreHttpRequest());
871876
}
872877

873878
private Employee getEmployeeModel() throws IOException {

0 commit comments

Comments
 (0)