1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
//
// ViewController.swift
// OHHTTPStubsDemo
//
// Created by Olivier Halligon on 18/04/2015.
// Copyright (c) 2015 AliSoftware. All rights reserved.
//
import UIKit
import OHHTTPStubs
class MainViewController: UIViewController {
////////////////////////////////////////////////////////////////////////////////
// MARK: - Outlets
@IBOutlet var delaySwitch: UISwitch!
@IBOutlet var textView: UITextView!
@IBOutlet var installTextStubSwitch: UISwitch!
@IBOutlet var imageView: UIImageView!
@IBOutlet var installImageStubSwitch: UISwitch!
////////////////////////////////////////////////////////////////////////////////
// MARK: - Init & Dealloc
override func viewDidLoad() {
super.viewDidLoad()
installTextStub(self.installTextStubSwitch)
installImageStub(self.installImageStubSwitch)
OHHTTPStubs.onStubActivation { (request: NSURLRequest!, stub: OHHTTPStubsDescriptor!) in
print("[OHHTTPStubs] Request to \(request.URL!) has been stubbed with \(stub.name)")
}
}
////////////////////////////////////////////////////////////////////////////////
// MARK: - Global stubs activation
@IBAction func toggleStubs(sender: UISwitch) {
OHHTTPStubs.setEnabled(sender.on)
self.delaySwitch.enabled = sender.on
self.installTextStubSwitch.enabled = sender.on
self.installImageStubSwitch.enabled = sender.on
let state = sender.on ? "and enabled" : "but disabled"
print("Installed (\(state)) stubs: \(OHHTTPStubs.allStubs)")
}
////////////////////////////////////////////////////////////////////////////////
// MARK: - Text Download and Stub
@IBAction func downloadText(sender: UIButton) {
sender.enabled = false
self.textView.text = nil
let urlString = "http://www.opensource.apple.com/source/Git/Git-26/src/git-htmldocs/git-commit.txt?txt"
let req = NSURLRequest(URL: NSURL(string: urlString)!)
NSURLConnection.sendAsynchronousRequest(req, queue: NSOperationQueue.mainQueue()) { (_, data, _) in
sender.enabled = true
if let receivedData = data, receivedText = NSString(data: receivedData, encoding: NSASCIIStringEncoding) {
self.textView.text = receivedText as String
}
}
}
weak var textStub: OHHTTPStubsDescriptor?
@IBAction func installTextStub(sender: UISwitch) {
if sender.on {
// Install
textStub = stub(isExtension("txt")) { _ in
let stubPath = OHPathForFile("stub.txt", self.dynamicType)
return fixture(stubPath!, headers: ["Content-Type":"text/plain"])
.requestTime(self.delaySwitch.on ? 2.0 : 0.0, responseTime:OHHTTPStubsDownloadSpeedWifi)
}
textStub?.name = "Text stub"
} else {
// Uninstall
OHHTTPStubs.removeStub(textStub!)
}
}
////////////////////////////////////////////////////////////////////////////////
// MARK: - Image Download and Stub
@IBAction func downloadImage(sender: UIButton) {
sender.enabled = false
self.imageView.image = nil
let urlString = "http://images.apple.com/support/assets/images/products/iphone/hero_iphone4-5_wide.png"
let req = NSURLRequest(URL: NSURL(string: urlString)!)
NSURLConnection.sendAsynchronousRequest(req, queue: NSOperationQueue.mainQueue()) { (_, data, _) in
sender.enabled = true
if let receivedData = data {
self.imageView.image = UIImage(data: receivedData)
}
}
}
weak var imageStub: OHHTTPStubsDescriptor?
@IBAction func installImageStub(sender: UISwitch) {
if sender.on {
// Install
imageStub = stub(isExtension("png") || isExtension("jpg") || isExtension("gif")) { _ in
let stubPath = OHPathForFile("stub.jpg", self.dynamicType)
return fixture(stubPath!, headers: ["Content-Type":"image/jpeg"])
.requestTime(self.delaySwitch.on ? 2.0 : 0.0, responseTime: OHHTTPStubsDownloadSpeedWifi)
}
imageStub?.name = "Image stub"
} else {
// Uninstall
OHHTTPStubs.removeStub(imageStub!)
}
}
////////////////////////////////////////////////////////////////////////////////
// MARK: - Cleaning
@IBAction func clearResults() {
self.textView.text = ""
self.imageView.image = nil
}
}
|