Skip to content

Commit 872e9e5

Browse files
authored
Merge pull request #21 from CodingAleCR/fix/uri-not-adding-params
Fix: Parameters not added to Uri objects.
2 parents 6d70381 + 03cf0a8 commit 872e9e5

File tree

7 files changed

+98
-17
lines changed

7 files changed

+98
-17
lines changed

example/lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ class WeatherApiInterceptor implements InterceptorContract {
284284
} catch (e) {
285285
print(e);
286286
}
287+
print(data.params);
287288
return data;
288289
}
289290

lib/http_client_with_interceptor.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,13 @@ class HttpClientWithInterceptor extends http.BaseClient {
132132
dynamic body,
133133
Encoding encoding,
134134
}) async {
135-
if (url is String) url = Uri.parse(addParametersToUrl(url, params));
135+
if (url is String) {
136+
url = Uri.parse(addParametersToStringUrl(url, params));
137+
} else if (url is Uri) {
138+
url = addParametersToUrl(url, params);
139+
} else {
140+
throw HttpInterceptorException("Malformed URL parameter");
141+
}
136142

137143
Request request = new Request(methodToString(method), url);
138144
if (headers != null) request.headers.addAll(headers);
@@ -150,7 +156,7 @@ class HttpClientWithInterceptor extends http.BaseClient {
150156
}
151157

152158
// Intercept request
153-
await _interceptRequest(request);
159+
request = await _interceptRequest(request);
154160

155161
var stream = requestTimeout == null
156162
? await send(request)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
3+
class HttpInterceptorException implements Exception {
4+
final message;
5+
6+
HttpInterceptorException([this.message]);
7+
8+
String toString() {
9+
if (message == null) return "Exception";
10+
return "Exception: $message";
11+
}
12+
}

lib/models/models.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export 'request_data.dart';
22
export 'response_data.dart';
3+
export 'http_interceptor_exception.dart';

lib/models/request_data.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class RequestData {
2323
}) : assert(method != null),
2424
assert(baseUrl != null);
2525

26-
String get url => addParametersToUrl(baseUrl, params);
26+
String get url => addParametersToStringUrl(baseUrl, params);
2727

2828
factory RequestData.fromHttpRequest(Request request) {
2929
var params = Map<String, String>();
@@ -42,7 +42,7 @@ class RequestData {
4242
}
4343

4444
Request toHttpRequest() {
45-
var reqUrl = Uri.parse(addParametersToUrl(baseUrl, params));
45+
var reqUrl = Uri.parse(addParametersToStringUrl(baseUrl, params));
4646

4747
Request request = new Request(methodToString(method), reqUrl);
4848

lib/utils.dart

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,48 @@
11
/// When having an URL as String and no parameters sent then it adds
22
/// them to the string.
3-
String addParametersToUrl(String url, Map<String, String> parameters) {
3+
String addParametersToStringUrl(String url, Map<String, String> parameters) {
4+
return buildUrlString(url, parameters);
5+
}
6+
7+
Uri addParametersToUrl(Uri url, Map<String, String> parameters) {
48
if (parameters == null) return url;
59

6-
String paramUrl = url;
7-
if (parameters != null && parameters.length > 0) {
8-
if (paramUrl.contains("?"))
9-
paramUrl += "&";
10-
else
11-
paramUrl += "?";
10+
String paramUrl = url.origin + url.path;
11+
12+
Map<String, String> newParameters = {};
13+
14+
url.queryParameters.forEach((key, value) {
15+
newParameters[key] = value;
16+
});
17+
18+
parameters.forEach((key, value) {
19+
newParameters[key] = value;
20+
});
21+
22+
return Uri.parse(buildUrlString(paramUrl, newParameters));
23+
}
24+
25+
String buildUrlString(String url, Map<String, String> parameters) {
26+
// Avoids unnecessary processing.
27+
if (parameters == null) return url;
28+
29+
// Check if there are parameters to add.
30+
if (parameters.length > 0) {
31+
// Checks if the string url already has parameters.
32+
if (url.contains("?")) {
33+
url += "&";
34+
} else {
35+
url += "?";
36+
}
37+
38+
// Concat every parameter to the string url.
1239
parameters.forEach((key, value) {
13-
paramUrl += "$key=$value&";
40+
url += "$key=$value&";
1441
});
15-
paramUrl = paramUrl.substring(0, paramUrl.length - 1);
42+
43+
// Remove last '&' character.
44+
url = url.substring(0, url.length - 1);
1645
}
17-
return paramUrl;
46+
47+
return url;
1848
}

test/utils_test.dart

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import 'package:flutter_test/flutter_test.dart';
22
import 'package:http_interceptor/utils.dart';
33

44
main() {
5-
group("addParametersToUrl", () {
5+
group("addParametersToStringUrl", () {
66
test("Adds parameters to a URL string without parameters", () {
77
// Arrange
88
String url = "https://www.google.com/helloworld";
99
Map<String, String> parameters = {"foo": "bar", "num": "0"};
1010

1111
// Act
12-
String parameterUrl = addParametersToUrl(url, parameters);
12+
String parameterUrl = addParametersToStringUrl(url, parameters);
1313

1414
// Assert
1515
expect(parameterUrl,
@@ -21,7 +21,7 @@ main() {
2121
Map<String, String> parameters = {"extra": "1", "extra2": "anotherone"};
2222

2323
// Act
24-
String parameterUrl = addParametersToUrl(url, parameters);
24+
String parameterUrl = addParametersToStringUrl(url, parameters);
2525

2626
// Assert
2727
expect(
@@ -30,4 +30,35 @@ main() {
3030
"https://www.google.com/helloworld?foo=bar&num=0&extra=1&extra2=anotherone"));
3131
});
3232
});
33+
group("addParametersToUrl", () {
34+
test("Add parameters to Uri Url without parameters", () {
35+
// Arrange
36+
String stringUrl = "https://www.google.com/helloworld";
37+
Map<String, String> parameters = {"foo": "bar", "num": "0"};
38+
Uri url = Uri.parse(stringUrl);
39+
40+
// Act
41+
Uri parameterUri = addParametersToUrl(url, parameters);
42+
43+
// Assert
44+
Uri expectedUrl = Uri.https("www.google.com", "/helloworld", parameters);
45+
expect(parameterUri, equals(expectedUrl));
46+
});
47+
test("Add parameters to Uri Url with parameters", () {
48+
// Arrange
49+
String authority = "www.google.com";
50+
String unencodedPath = "/helloworld";
51+
Map<String, String> someParameters = {"foo": "bar"};
52+
Map<String, String> otherParameters = {"num": "0"};
53+
Uri url = Uri.https(authority, unencodedPath, someParameters);
54+
55+
// Act
56+
Uri parameterUri = addParametersToUrl(url, otherParameters);
57+
58+
// Assert
59+
Map<String, String> allParameters = {"foo": "bar", "num": "0"};
60+
Uri expectedUrl = Uri.https("www.google.com", "/helloworld", allParameters);
61+
expect(parameterUri, equals(expectedUrl));
62+
});
63+
});
3364
}

0 commit comments

Comments
 (0)