Skip to content

Commit

Permalink
Fixes badly forged URL crash + unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
s4cha committed Sep 17, 2021
1 parent 4234a81 commit 738c718
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Sources/Networking/NetworkingRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ public class NetworkingRequest: NSObject {
urlString = getURLWithParams()
}

let url = URL(string: urlString)!
guard let url = URL(string: urlString) else {
return nil
}
var request = URLRequest(url: url)

if httpVerb != .get && multipartData == nil {
Expand Down
20 changes: 20 additions & 0 deletions Tests/NetworkingTests/NetworkingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ final class NetworkingTests: XCTestCase {
}).store(in: &cancellables)
waitForExpectations(timeout: 3, handler: nil)
}

func testBadURLDoesntCrash() {
let exp = expectation(description: "call")
let client = NetworkingClient(baseURL: "https://jsonplaceholder.typicode.com")
client.get("/forge a bad url")
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
print("finished")
case .failure(let error):
if let e = error as? NetworkingError, e.status == .unableToParseRequest {
exp.fulfill()
}
}
},
receiveValue: { (json: Any) in
print(json)
}).store(in: &cancellables)
waitForExpectations(timeout: 1, handler: nil)
}
//
// func testGetDecodableModels() {
// let exp = expectation(description: "call")
Expand Down

0 comments on commit 738c718

Please sign in to comment.