From c95674b759318893ccc1875646a36cb294c6a4bb Mon Sep 17 00:00:00 2001 From: Vadym Markov Date: Sun, 20 Aug 2017 19:53:57 +0200 Subject: [PATCH] Fix: construct request URL with query parameters --- MalibuTests/Specs/Request/RequestSpec.swift | 11 +++++++++++ Sources/Request/Request.swift | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/MalibuTests/Specs/Request/RequestSpec.swift b/MalibuTests/Specs/Request/RequestSpec.swift index 4c0b739..12b8380 100644 --- a/MalibuTests/Specs/Request/RequestSpec.swift +++ b/MalibuTests/Specs/Request/RequestSpec.swift @@ -98,6 +98,17 @@ class RequestSpec: QuickSpec { } } + context("with base URL without slash and query parameters") { + it("does not throw an error and returns created URLRequest") { + request = Request.post("about?q=1") + + expect { + urlRequest = try request.toUrlRequest(baseUrl: "http://api.loc") + }.toNot(throwError()) + expect(urlRequest.url?.absoluteString).to(equal("http://api.loc/about?q=1")) + } + } + context("with additional headers") { it("returns created URLRequest with new header added") { let headers = ["foo": "bar", "key": "bar"] diff --git a/Sources/Request/Request.swift b/Sources/Request/Request.swift index 6ffdce4..e4fddf7 100644 --- a/Sources/Request/Request.swift +++ b/Sources/Request/Request.swift @@ -287,12 +287,17 @@ public extension Request { let url: URL? if let baseUrl = baseUrl { + var base = baseUrl.urlString + if !base.hasSuffix("/") { + base.append("/") + } + var path = resource.urlString if path.hasPrefix("/") { path.remove(at: path.startIndex) } - url = URL(string: baseUrl.urlString)?.appendingPathComponent(path) + url = URL(string: "\(base)\(path)") } else { url = URL(string: resource.urlString) }