|
1 | 1 | import XCTest |
| 2 | +import Foundation |
2 | 3 | @testable import HTTPEngine |
3 | 4 |
|
4 | 5 | final class HTTPEngineTests: XCTestCase { |
5 | | - func testExample() { |
6 | | - // This is an example of a functional test case. |
7 | | - // Use XCTAssert and related functions to verify your tests produce the correct |
8 | | - // results. |
9 | | - XCTAssertEqual(HTTPEngine().text, "Hello, World!") |
| 6 | + |
| 7 | + func testBuildRequestContainsAcceptEncodingByDefault() { |
| 8 | + HTTPMethod.allCases.forEach { |
| 9 | + let request = HTTPEngine().buildRequest(method: $0, url: URL(string: "www.google.com")!) |
| 10 | + XCTAssertEqual(request.allHTTPHeaderFields?["Accept-Encoding"], "gzip;q=1.0,compress;q=0.5") |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + func testBuildRequestsOverridesAcceptEncodingHeader() { |
| 15 | + let request = HTTPEngine() |
| 16 | + .buildRequest( |
| 17 | + method: .get, |
| 18 | + url: URL(string: "www.google.com")!, |
| 19 | + header: ["Accept-Encoding": "something"]) |
| 20 | + XCTAssertEqual(request.allHTTPHeaderFields?["Accept-Encoding"], "something") |
| 21 | + } |
| 22 | + |
| 23 | + func testPostPatchPutContainDefaultContentTypeHeader() { |
| 24 | + let methods: [HTTPMethod] = [.post, .patch, .put] |
| 25 | + methods.forEach { |
| 26 | + let request = HTTPEngine().buildRequest(method: $0, url: URL(string: "www.google.com")!) |
| 27 | + XCTAssertEqual(request.allHTTPHeaderFields?["Content-Type"], "application/json") |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + func testNonPostPatchPutMethodsDoNotContainDefaultContentTypeHeader() { |
| 32 | + let methods: [HTTPMethod] = [.get, .delete] |
| 33 | + methods.forEach { |
| 34 | + let request = HTTPEngine().buildRequest(method: $0, url: URL(string: "www.google.com")!) |
| 35 | + XCTAssertNil(request.allHTTPHeaderFields?["Content-Type"]) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + func testBuildRequestOverridesContentTypeHeader() { |
| 41 | + let methods: [HTTPMethod] = HTTPMethod.allCases |
| 42 | + methods.forEach { |
| 43 | + let request = HTTPEngine() |
| 44 | + .buildRequest( |
| 45 | + method: $0, |
| 46 | + url: URL(string: "www.google.com")!, |
| 47 | + header: ["Content-Type": "something"] |
| 48 | + ) |
| 49 | + XCTAssertEqual(request.allHTTPHeaderFields?["Content-Type"], "something") |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + func testBuildRequestAppliesBodyToRequest() { |
| 54 | + let request = HTTPEngine().buildRequest(method: .get, url: URL(string: "www.google.com")!, body: "".data(using: .utf8)!) |
| 55 | + XCTAssertNotNil(request.httpBody) |
| 56 | + } |
| 57 | + |
| 58 | + func testBuildRequestAppliesMethodToRequest() { |
| 59 | + let request = HTTPEngine().buildRequest(method: .get, url: URL(string: "www.google.com")!) |
| 60 | + XCTAssertEqual(request.httpMethod, "GET") |
10 | 61 | } |
11 | 62 |
|
| 63 | + |
12 | 64 | static var allTests = [ |
13 | | - ("testExample", testExample), |
| 65 | + ("Default Accept Encoding Header", testBuildRequestContainsAcceptEncodingByDefault), |
| 66 | + ("Override Accept Encoding Header", testBuildRequestsOverridesAcceptEncodingHeader), |
| 67 | + ("Put Patch Post contain default Content Type header", testPostPatchPutContainDefaultContentTypeHeader), |
| 68 | + ("Non Put Patch Post do not contain content type header", testNonPostPatchPutMethodsDoNotContainDefaultContentTypeHeader), |
| 69 | + ("Build request overrides content type header", testBuildRequestOverridesContentTypeHeader), |
| 70 | + ("Build Request applies body to request", testBuildRequestAppliesBodyToRequest), |
| 71 | + ("Build Request applies Method to request", testBuildRequestAppliesMethodToRequest) |
14 | 72 | ] |
15 | 73 | } |
0 commit comments