summaryrefslogtreecommitdiff
path: root/test/ios/OHHTTPStubs/Examples/Swift/MainViewController.swift
blob: e983f11f39c06049c5661e0b13ea2c1f00ff12ae (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
//  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
            println("[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"
        println("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
            let receivedText = NSString(data: data, encoding: NSASCIIStringEncoding)
            self.textView.text = receivedText! as String
        }
    }

    weak var textStub: OHHTTPStubsDescriptor?
    @IBAction func installTextStub(sender: UISwitch) {
        if sender.on {
            // Install

            // - we use an inline closure for the test predicate with shorthand argument names
            // - we also use the "trailing closure" syntax of Swift for convenience
            // - we don't care about the 'request' parameter in the trailing closure so we use '_'
            textStub = OHHTTPStubs.stubRequestsPassingTest({$0.URL!.pathExtension == "txt"}) { _ in
                let stubPath = OHPathForFile("stub.txt", self.dynamicType)
                return OHHTTPStubsResponse(
                    fileAtPath: stubPath!,
                    statusCode: 200,
                    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
            self.imageView.image = UIImage(data: data)
        }
    }
    
    weak var imageStub: OHHTTPStubsDescriptor?
    @IBAction func installImageStub(sender: UISwitch) {
        if sender.on {
            // Install
            
            let isImage = { (request: NSURLRequest!) -> Bool in
                contains(["png","jpg","jpeg","gif"], request.URL?.pathExtension ?? "")
            }
            // - we use a separate block 'isImage' for the 'test' predicate for code readability
            // - we use the "trailing closure" syntax of Swift for convenience
            // - we don't care about the 'request' parameter in the trailing closure so we use '_'
            imageStub = OHHTTPStubs.stubRequestsPassingTest(isImage) { _ in
                let stubPath = OHPathForFile("stub.jpg", self.dynamicType)
                return OHHTTPStubsResponse(
                    fileAtPath: stubPath!,
                    statusCode: 200,
                    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
    }
    
}