Skip to content

Commit

Permalink
add timeout configure
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlui committed Feb 20, 2017
1 parent 16f8cd4 commit 0c7e9a1
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Pitaya.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
C8E781F21D94D8DF0083A9F7 /* JSONND+ArrayLiteralConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8E781EE1D94D8DF0083A9F7 /* JSONND+ArrayLiteralConvertible.swift */; };
C8E781F31D94D8DF0083A9F7 /* JSONND+DictionaryLiteralConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8E781EF1D94D8DF0083A9F7 /* JSONND+DictionaryLiteralConvertible.swift */; };
C8E781F41D94D8DF0083A9F7 /* JSONNDModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8E781F01D94D8DF0083A9F7 /* JSONNDModel.swift */; };
C8F02A271E5AFE2F0019CD22 /* SetTimeout.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8F02A261E5AFE2F0019CD22 /* SetTimeout.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -79,6 +80,7 @@
C8E781EE1D94D8DF0083A9F7 /* JSONND+ArrayLiteralConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "JSONND+ArrayLiteralConvertible.swift"; path = "Source/JSONNeverDie/JSONND+ArrayLiteralConvertible.swift"; sourceTree = SOURCE_ROOT; };
C8E781EF1D94D8DF0083A9F7 /* JSONND+DictionaryLiteralConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "JSONND+DictionaryLiteralConvertible.swift"; path = "Source/JSONNeverDie/JSONND+DictionaryLiteralConvertible.swift"; sourceTree = SOURCE_ROOT; };
C8E781F01D94D8DF0083A9F7 /* JSONNDModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = JSONNDModel.swift; path = Source/JSONNeverDie/JSONNDModel.swift; sourceTree = SOURCE_ROOT; };
C8F02A261E5AFE2F0019CD22 /* SetTimeout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SetTimeout.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -157,6 +159,7 @@
C8ABB72C1BC6AF1200C4C897 /* BasicAuth.swift */,
8E610C4D1BD0A95D00CF9982 /* AddHTTPHeader.swift */,
C84B227F1BCA1D0B00594876 /* SetHTTPBodyRaw.swift */,
C8F02A261E5AFE2F0019CD22 /* SetTimeout.swift */,
C88EE1671BC3A6F10033D3B0 /* AddSSLPinning.swift */,
C855F3711BC8247B00170211 /* ResponseJSON.swift */,
B884D1A71E07B8EB0008F5AB /* [email protected] */,
Expand Down Expand Up @@ -324,6 +327,7 @@
C8ABB7291BC6A89600C4C897 /* WithStringParams.swift in Sources */,
8E610C4E1BD0A95D00CF9982 /* AddHTTPHeader.swift in Sources */,
C8ABB7271BC6A11600C4C897 /* BasicTests.swift in Sources */,
C8F02A271E5AFE2F0019CD22 /* SetTimeout.swift in Sources */,
C84B22801BCA1D0B00594876 /* SetHTTPBodyRaw.swift in Sources */,
C8ABB72D1BC6AF1200C4C897 /* BasicAuth.swift in Sources */,
C8B35A061C2D1DBF0016A222 /* Control.swift in Sources */,
Expand Down
Binary file not shown.
40 changes: 40 additions & 0 deletions PitayaTests/SetTimeout.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// SetTimeout.swift
// Pitaya
//
// Created by 吕文翰 on 17/2/20.
// Copyright © 2017年 http://lvwenhan.com. All rights reserved.
//

import XCTest
import Pitaya

class SetTimeout: BaseTestCase {

func testSetTimeoutSuccess() {
let expectation = self.expectation(description: "testSetTimeoutSuccess")

Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/delay/5", timeout: 8)
.onNetworkError({ (error) -> Void in
XCTAssert(false, error.localizedDescription)
})
.responseJSON { (json, response) -> Void in
XCTAssertEqual(json["url"].stringValue, "http://httpbin.org/delay/5")
expectation.fulfill()
}
waitForExpectations(timeout: self.defaultTimeout, handler: nil)
}

func testSetTimeoutFail() {
let expectation = self.expectation(description: "testSetTimeoutFail")

Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/delay/5", timeout: 2)
.onNetworkError({ (error) -> Void in
expectation.fulfill()
})
.responseJSON { (json, response) -> Void in
XCTAssertEqual(json["url"].stringValue, "http://httpbin.org/delay/5")
}
waitForExpectations(timeout: self.defaultTimeout, handler: nil)
}
}
10 changes: 8 additions & 2 deletions Source/Pitaya.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ open class Pitaya {
/**
the only init method to fire a HTTP / HTTPS request
- parameter method: the HTTP method you want
- parameter url: the url you want
- parameter method: the HTTP method you want
- parameter url: the url you want
- parameter timeout: time out setting
- returns: a Pitaya object
*/
Expand All @@ -51,6 +52,11 @@ open class Pitaya {
p.pitayaManager = PitayaManager.build(method, url: url)
return p
}
open static func build(HTTPMethod method: HTTPMethod, url: String, timeout: Double) -> Pitaya {
let p = Pitaya()
p.pitayaManager = PitayaManager.build(method, url: url, timeout: timeout)
return p
}

/**
add params to self (Pitaya object)
Expand Down
4 changes: 2 additions & 2 deletions Source/PitayaManager+Static.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extension PitayaManager {
- returns: self (PitayaManager object)
*/
static func build(_ method: HTTPMethod, url: String) -> PitayaManager {
return PitayaManager(url: url, method: method)
static func build(_ method: HTTPMethod, url: String, timeout: Double = 60.0) -> PitayaManager {
return PitayaManager(url: url, method: method, timeout: timeout)
}
}
6 changes: 4 additions & 2 deletions Source/PitayaManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ class PitayaManager: NSObject, URLSessionDelegate {
return "Pitaya"
}()

init(url: String, method: HTTPMethod!) {
init(url: String, method: HTTPMethod!, timeout: Double = 60.0) {
self.url = url
self.request = URLRequest(url: URL(string: url)!)
self.method = method.rawValue

super.init()
// setup a session with delegate to self
self.session = Foundation.URLSession(configuration: Foundation.URLSession.shared.configuration, delegate: self, delegateQueue: Foundation.URLSession.shared.delegateQueue)
let sessionConfiguration = Foundation.URLSession.shared.configuration
sessionConfiguration.timeoutIntervalForRequest = timeout
self.session = Foundation.URLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: Foundation.URLSession.shared.delegateQueue)
}
func addSSLPinning(LocalCertData dataArray: [Data], SSLValidateErrorCallBack: (()->Void)? = nil) {
self.localCertDataArray = dataArray
Expand Down

0 comments on commit 0c7e9a1

Please sign in to comment.