Skip to content

Commit

Permalink
Merge pull request #77 from hyperoslo/fix/construct-url-request
Browse files Browse the repository at this point in the history
Fix: construct request URL with query parameters
  • Loading branch information
vadymmarkov authored Aug 21, 2017
2 parents 7fff266 + c95674b commit d8cc9a4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 11 additions & 0 deletions MalibuTests/Specs/Request/RequestSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
7 changes: 6 additions & 1 deletion Sources/Request/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit d8cc9a4

Please sign in to comment.