11import XCTest
22import Foundation
3+ import OHHTTPStubs
4+ import OHHTTPStubsSwift
35@testable import HTTPEngine
46
57final class HTTPEngineTests : XCTestCase {
68
9+ // MARK: - Build Request
10+
711 func testBuildRequestContainsAcceptEncodingByDefault( ) {
812 HTTPMethod . allCases. forEach {
913 let request = HTTPEngine ( ) . buildRequest ( method: $0, url: URL ( string: " www.google.com " ) !)
@@ -59,6 +63,64 @@ final class HTTPEngineTests: XCTestCase {
5963 let request = HTTPEngine ( ) . buildRequest ( method: . get, url: URL ( string: " www.google.com " ) !)
6064 XCTAssertEqual ( request. httpMethod, " GET " )
6165 }
66+
67+ // MARK: - Make Request
68+
69+ func testMakeRequestFailsWithUnexpectedResponseCode( ) {
70+ stub ( condition: isHost ( " google.com " ) && isMethodGET ( ) ) { _ in
71+ HTTPStubsResponse ( jsonObject: [ : ] , statusCode: 200 , headers: nil )
72+ }
73+
74+ HTTPEngine ( )
75+ . makeRequest ( method: . get, url: " https://google.com " , validator: { $0 == 202 } )
76+ . assertError ( test: self ) {
77+ switch $0 {
78+ case Errors . Response . unexpectedStatusCode( let response) :
79+ XCTAssertEqual ( response. statusCode, 200 )
80+ default : XCTFail ( #function)
81+ }
82+ }
83+ }
84+
85+ func testMakeRequestSucceedsIn200RangeByDefault( ) {
86+ for statusCode in ( 200 ... 299 ) {
87+ stub ( condition: isHost ( " google.com " ) && isMethodGET ( ) ) { _ in
88+ HTTPStubsResponse ( jsonObject: [ " key " : " value " ] , statusCode: Int32 ( statusCode) , headers: nil )
89+ }
90+
91+ HTTPEngine ( )
92+ . makeRequest ( method: . get, url: " https://google.com " )
93+ . assertResult ( test: self ) {
94+ XCTAssertEqual (
95+ [ " key " : " value " ] ,
96+ try ? JSONSerialization . jsonObject ( with: $0, options: [ ] ) as? [ String : String ]
97+ )
98+ }
99+ }
100+ }
101+
102+
103+ func testMakeRequestFailsOutsideOf200RangeByDefault( ) {
104+ stub ( condition: isHost ( " google.com " ) && isMethodGET ( ) ) { _ in
105+ HTTPStubsResponse ( jsonObject: [ : ] , statusCode: 300 , headers: nil )
106+ }
107+
108+ HTTPEngine ( )
109+ . makeRequest ( method: . get, url: " https://google.com " )
110+ . assertError ( test: self ) {
111+ XCTAssertEqual ( $0. localizedDescription, Errors . Response. redirect ( 300 ) . localizedDescription)
112+ }
113+
114+ stub ( condition: isHost ( " google.com " ) && isMethodGET ( ) ) { _ in
115+ HTTPStubsResponse ( jsonObject: [ : ] , statusCode: 199 , headers: nil )
116+ }
117+
118+ HTTPEngine ( )
119+ . makeRequest ( method: . get, url: " https://google.com " )
120+ . assertError ( test: self ) {
121+ XCTAssertEqual ( $0. localizedDescription, Errors . Response. errorWith ( statusCode: 199 ) ? . localizedDescription)
122+ }
123+ }
62124
63125
64126 static var allTests = [
@@ -68,6 +130,10 @@ final class HTTPEngineTests: XCTestCase {
68130 ( " Non Put Patch Post do not contain content type header " , testNonPostPatchPutMethodsDoNotContainDefaultContentTypeHeader) ,
69131 ( " Build request overrides content type header " , testBuildRequestOverridesContentTypeHeader) ,
70132 ( " Build Request applies body to request " , testBuildRequestAppliesBodyToRequest) ,
71- ( " Build Request applies Method to request " , testBuildRequestAppliesMethodToRequest)
133+ ( " Build Request applies Method to request " , testBuildRequestAppliesMethodToRequest) ,
134+ ( " Make Request Fails with invalid status code " ,
135+ testMakeRequestFailsWithUnexpectedResponseCode) ,
136+ ( " Make Request Succeeds in 200 range by default " , testMakeRequestSucceedsIn200RangeByDefault) ,
137+ ( " Make request fails outside of 200 range by default " , testMakeRequestFailsOutsideOf200RangeByDefault)
72138 ]
73139}
0 commit comments