Skip to content

Commit

Permalink
feat: use baseUrl in RequestOptions as 1st priority
Browse files Browse the repository at this point in the history
add smaple code and unit test

fix cfug#385
  • Loading branch information
trevorwang committed Aug 15, 2019
1 parent cad527f commit 29a2621
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions example/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ main() async {
contentType: ContentType.parse("application/x-www-form-urlencoded")),
);
print(response.data);

response = await dio.request("/get",
options: RequestOptions(baseUrl: "http://httpbin.org/"));
print(response.data);
}
3 changes: 2 additions & 1 deletion package_src/lib/src/dio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -947,10 +947,11 @@ class Dio {
Options opt, String url, data, Map<String, dynamic> queryParameters) {
var query = (Map<String, dynamic>.from(options.queryParameters ?? {}))
..addAll(queryParameters ?? {});
final optBaseUrl = (opt is RequestOptions) ? opt.baseUrl: null;
return RequestOptions(
method: (opt.method ?? options.method)?.toUpperCase() ?? "GET",
headers: (Map.from(options.headers))..addAll(opt.headers),
baseUrl: options.baseUrl ?? "",
baseUrl: optBaseUrl ?? options.baseUrl ?? "",
path: url,
data: data,
connectTimeout: opt.connectTimeout ?? options.connectTimeout ?? 0,
Expand Down
19 changes: 19 additions & 0 deletions package_src/test/dio_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,23 @@ void main() {
]);
});
});

group("basic tests", () {
Dio dio;
setUp(() {
dio = Dio();
// dio.options.baseUrl = MockAdapter.mockBase;
dio.options.headers = {'User-Agent': 'dartisan', 'XX': '8'};
dio.httpClientAdapter = MockAdapter();
});
test("base url in request options", () async {
Response response;
response = await dio.get(
"/test",
queryParameters: {"id": '12', "name": "wendu"},
options: RequestOptions(baseUrl: MockAdapter.mockBase),
);
expect(response.data["errCode"], 0);
});
});
}

0 comments on commit 29a2621

Please sign in to comment.