Class: WebStub::Protocol
- Inherits:
-
NSURLProtocol
- Object
- NSURLProtocol
- WebStub::Protocol
- Defined in:
- lib/webstub/protocol.rb
Class Method Summary collapse
- .add_stub(*args) ⇒ Object
- .canInitWithRequest(request) ⇒ Object
- .canonicalRequestForRequest(request) ⇒ Object
- .disable_network_access! ⇒ Object
- .enable_network_access! ⇒ Object
- .network_access_allowed? ⇒ Boolean
- .reset_stubs ⇒ Object
Instance Method Summary collapse
- #completeLoading ⇒ Object
- #initWithRequest(request, cachedResponse: response, client: client) ⇒ Object
- #startLoading ⇒ Object
- #stopLoading ⇒ Object
Class Method Details
.add_stub(*args) ⇒ Object
3 4 5 |
# File 'lib/webstub/protocol.rb', line 3 def self.add_stub(*args) registry.add_stub(*args) end |
.canInitWithRequest(request) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/webstub/protocol.rb', line 7 def self.canInitWithRequest(request) return false unless spec_mode? return false unless supported?(request) if stub_for(request) return true end ! network_access_allowed? end |
.canonicalRequestForRequest(request) ⇒ Object
18 19 20 |
# File 'lib/webstub/protocol.rb', line 18 def self.canonicalRequestForRequest(request) request end |
.disable_network_access! ⇒ Object
22 23 24 |
# File 'lib/webstub/protocol.rb', line 22 def self.disable_network_access! @network_access = false end |
.enable_network_access! ⇒ Object
26 27 28 |
# File 'lib/webstub/protocol.rb', line 26 def self.enable_network_access! @network_access = true end |
.network_access_allowed? ⇒ Boolean
30 31 32 |
# File 'lib/webstub/protocol.rb', line 30 def self.network_access_allowed? @network_access.nil? ? true : @network_access end |
.reset_stubs ⇒ Object
34 35 36 |
# File 'lib/webstub/protocol.rb', line 34 def self.reset_stubs registry.reset end |
Instance Method Details
#completeLoading ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/webstub/protocol.rb', line 47 def completeLoading response = NSHTTPURLResponse.alloc.initWithURL(request.URL, statusCode:@stub.response_status_code, HTTPVersion:"HTTP/1.1", headerFields:@stub.response_headers) @stub.requests += 1 if @stub.error? client.URLProtocol(self, didFailWithError: @stub.response_error) return end if @stub.redirects? url = NSURL.URLWithString(@stub.response_headers["Location"]) if request.URL == url # Can't redirect to same URL. Since iOS 10, it will cause a crash. return end redirect_request = NSURLRequest.requestWithURL(url) client.URLProtocol(self, wasRedirectedToRequest: redirect_request, redirectResponse: response) unless @stub = self.class.stub_for(redirect_request) error = NSError.errorWithDomain("WebStub", code:0, userInfo:{ NSLocalizedDescriptionKey: "network access is not permitted!"}) client.URLProtocol(self, didFailWithError:error) return end @timer = NSTimer.scheduledTimerWithTimeInterval(@stub.response_delay, target:self, selector: :completeLoading, userInfo:nil, repeats:false) return end client.URLProtocol(self, didReceiveResponse:response, cacheStoragePolicy:NSURLCacheStorageNotAllowed) client.URLProtocol(self, didLoadData: @stub.response_body.is_a?(NSData) ? @stub.response_body : @stub.response_body.dataUsingEncoding(NSUTF8StringEncoding)) client.URLProtocolDidFinishLoading(self) end |
#initWithRequest(request, cachedResponse: response, client: client) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/webstub/protocol.rb', line 38 def initWithRequest(request, cachedResponse:response, client: client) if super @stub = nil @timer = nil end self end |
#startLoading ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/webstub/protocol.rb', line 86 def startLoading request = self.request client = self.client unless @stub = self.class.stub_for(self.request) error = NSError.errorWithDomain("WebStub", code:0, userInfo:{ NSLocalizedDescriptionKey: "network access is not permitted!"}) client.URLProtocol(self, didFailWithError:error) return end @stub.do_callback(self.request.allHTTPHeaderFields || {}, self.class.parse_body(request)) @timer = NSTimer.scheduledTimerWithTimeInterval(@stub.response_delay, target:self, selector: :completeLoading, userInfo:nil, repeats:false) end |
#stopLoading ⇒ Object
101 102 103 104 105 |
# File 'lib/webstub/protocol.rb', line 101 def stopLoading if @timer @timer.invalidate end end |