summaryrefslogtreecommitdiff
path: root/platform/ios/test/OHHTTPStubs/OHHTTPStubs/UnitTests/Test Suites/SwiftHelpersTests.swift
blob: 3d2f1c3577bcb2adaa8eaa6cfc5cf50669df20e9 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
//  SwiftHelpersTests.swift
//  OHHTTPStubs
//
//  Created by Olivier Halligon on 20/09/2015.
//  Copyright © 2015 AliSoftware. All rights reserved.
//

import Foundation
import XCTest
import OHHTTPStubs

class SwiftHelpersTests : XCTestCase {
    
    func testIsScheme() {
        let matcher = isScheme("foo")
        
        let urls = [
            "foo:": true,
            "foo://": true,
            "foo://bar/baz": true,
            "bar://": false,
            "bar://foo/": false,
            "foobar://": false
        ]
        
        for (url, result) in urls {
            let req = NSURLRequest(URL: NSURL(string: url)!)
            XCTAssert(matcher(req) == result, "isScheme(\"foo\") matcher failed when testing url \(url)")
        }
    }
    
    func testIsHost() {
        let matcher = isHost("foo")
        
        let urls = [
            "foo:": false,
            "foo://": false,
            "foo://bar/baz": false,
            "bar://foo": true,
            "bar://foo/baz": true,
        ]
        
        for (url, result) in urls {
            let req = NSURLRequest(URL: NSURL(string: url)!)
            XCTAssert(matcher(req) == result, "isHost(\"foo\") matcher failed when testing url \(url)")
        }
    }
    
    func testIsPath_absoluteURL() {
        testIsPath("/foo/bar/baz", isAbsoluteMatcher: true)
    }
    
    func testIsPath_relativeURL() {
        testIsPath("foo/bar/baz", isAbsoluteMatcher: false)
    }
    
    func testIsPath(path: String, isAbsoluteMatcher: Bool) {
        let matcher = isPath(path)
        
        let urls = [
            // Absolute URLs
            "scheme:": false,
            "scheme://": false,
            "scheme://foo/bar/baz": false,
            "scheme://host/foo/bar": false,
            "scheme://host/foo/bar/baz": isAbsoluteMatcher,
            "scheme://host/foo/bar/baz?q=1": isAbsoluteMatcher,
            "scheme://host/foo/bar/baz#anchor": isAbsoluteMatcher,
            "scheme://host/foo/bar/baz;param": isAbsoluteMatcher,
            "scheme://host/foo/bar/baz/wizz": false,
            "scheme://host/path#/foo/bar/baz": false,
            "scheme://host/path?/foo/bar/baz": false,
            "scheme://host/path;/foo/bar/baz": false,
            // Relative URLs
            "foo/bar/baz": !isAbsoluteMatcher,
            "foo/bar/baz?q=1": !isAbsoluteMatcher,
            "foo/bar/baz#anchor": !isAbsoluteMatcher,
            "foo/bar/baz;param": !isAbsoluteMatcher,
            "foo/bar/baz/wizz": false,
            "path#/foo/bar/baz": false,
            "path?/foo/bar/baz": false,
            "path;/foo/bar/baz": false,
        ]
        
        for (url, result) in urls {
            let req = NSURLRequest(URL: NSURL(string: url)!)
            let p = req.URL?.path
            print("URL: \(url) -> Path: \(p)")
            XCTAssert(matcher(req) == result, "isPath(\"\(path)\" matcher failed when testing url \(url)")
        }
    }
    
    func testIsExtension() {
        let matcher = isExtension("txt")
        
        let urls = [
            "txt:": false,
            "txt://": false,
            "txt://txt/txt/txt": false,
            "scheme://host/foo/bar.png": false,
            "scheme://host/foo/bar.txt": true,
            "scheme://host/foo/bar.txt?q=1": true,
            "scheme://host/foo/bar.baz?q=wizz.txt": false,
        ]
        
        for (url, result) in urls {
            let req = NSURLRequest(URL: NSURL(string: url)!)
            XCTAssert(matcher(req) == result, "isExtension(\"txt\") matcher failed when testing url \(url)")
        }

    }
    
    func testContainsQueryParams() {
        let params: [String: String?] = ["q":"test", "lang":"en", "empty":"", "flag":nil]
        let matcher = containsQueryParams(params)
        
        let urls = [
            "foo://bar": false,
            "foo://bar?q=test": false,
            "foo://bar?lang=en": false,
            "foo://bar#q=test&lang=en&empty=&flag": false,
            "foo://bar#lang=en&empty=&flag&q=test": false,
            "foo://bar;q=test&lang=en&empty=&flag": false,
            "foo://bar;lang=en&empty=&flag&q=test": false,

            "foo://bar?q=test&lang=en&empty=&flag": true,
            "foo://bar?lang=en&flag&empty=&q=test": true,
            "foo://bar?q=test&lang=en&empty=&flag#anchor": true,
            "foo://bar?q=test&lang=en&empty&flag": false, // key "empty" with no value is matched against nil, not ""
            "foo://bar?q=test&lang=en&empty=&flag=": false, // key "flag" with empty value is matched against "", not nil
            "foo://bar?q=en&lang=test&empty=&flag": false, // param keys and values mismatch
            "foo://bar?q=test&lang=en&empty=&flag&&wizz=fuzz": true,
            "foo://bar?wizz=fuzz&empty=&lang=en&flag&&q=test": true,
            "?q=test&lang=en&empty=&flag": true,
            "?lang=en&flag&empty=&q=test": true,
        ]
        
        for (url, result) in urls {
            let req = NSURLRequest(URL: NSURL(string: url)!)
            XCTAssert(matcher(req) == result, "containsQueryParams(\"\(params)\") matcher failed when testing url \(url)")
        }
    }
    
    let sampleURLs = [
        // Absolute URLs
        "scheme:",
        "scheme://",
        "scheme://foo/bar/baz",
        "scheme://host/foo/bar",
        "scheme://host/foo/bar/baz",
        "scheme://host/foo/bar/baz?q=1",
        "scheme://host/foo/bar/baz#anchor",
        "scheme://host/foo/bar/baz;param",
        "scheme://host/foo/bar/baz/wizz",
        "scheme://host/path#/foo/bar/baz",
        "scheme://host/path?/foo/bar/baz",
        "scheme://host/path;/foo/bar/baz",
        // Relative URLs
        "foo/bar/baz",
        "foo/bar/baz?q=1",
        "foo/bar/baz#anchor",
        "foo/bar/baz;param",
        "foo/bar/baz/wizz",
        "path#/foo/bar/baz",
        "path?/foo/bar/baz",
        "path;/foo/bar/baz"
    ]

    let trueMatcher: OHHTTPStubsTestBlock = { _ in return true }
    let falseMatcher: OHHTTPStubsTestBlock = { _ in return false }

    func testOrOperator() {
        for url in sampleURLs {
            let req = NSURLRequest(URL: NSURL(string: url)!)
            XCTAssert((trueMatcher || trueMatcher)(req) == true, "trueMatcher || trueMatcher should result in a trueMatcher")
            XCTAssert((trueMatcher || falseMatcher)(req) == true, "trueMatcher || falseMatcher should result in a trueMatcher")
            XCTAssert((falseMatcher || trueMatcher)(req) == true, "falseMatcher || trueMatcher should result in a trueMatcher")
            XCTAssert((falseMatcher || falseMatcher)(req) == false, "falseMatcher || falseMatcher should result in a falseMatcher")
        }
    }
    
    func testAndOperator() {
        for url in sampleURLs {
            let req = NSURLRequest(URL: NSURL(string: url)!)
            XCTAssert((trueMatcher && trueMatcher)(req) == true, "trueMatcher && trueMatcher should result in a trueMatcher")
            XCTAssert((trueMatcher && falseMatcher)(req) == false, "trueMatcher && falseMatcher should result in a falseMatcher")
            XCTAssert((falseMatcher && trueMatcher)(req) == false, "falseMatcher && trueMatcher should result in a falseMatcher")
            XCTAssert((falseMatcher && falseMatcher)(req) == false, "falseMatcher && falseMatcher should result in a falseMatcher")
        }
    }
    
    func testNotOperator() {
        for url in sampleURLs {
            let req = NSURLRequest(URL: NSURL(string: url)!)
            XCTAssert((!trueMatcher)(req) == false, "!trueMatcher should result in a falseMatcher")
            XCTAssert((!falseMatcher)(req) == true, "!falseMatcher should result in a trueMatcher")
        }
    }
}