~ps-jenkins/cordova-ubuntu-tests/latestsnapshot-2.11+14.04.20131106-0ubuntu1

« back to all changes in this revision

Viewing changes to www/autotest/tests/whitelist.tests.js

  • Committer: VĂ­ctor R. Ruiz
  • Date: 2013-07-25 13:09:34 UTC
  • Revision ID: victor.ruiz@canonical.com-20130725130934-d4q95mh8eehbv363
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 * Licensed to the Apache Software Foundation (ASF) under one
 
4
 * or more contributor license agreements.  See the NOTICE file
 
5
 * distributed with this work for additional information
 
6
 * regarding copyright ownership.  The ASF licenses this file
 
7
 * to you under the Apache License, Version 2.0 (the
 
8
 * "License"); you may not use this file except in compliance
 
9
 * with the License.  You may obtain a copy of the License at
 
10
 *
 
11
 *   http://www.apache.org/licenses/LICENSE-2.0
 
12
 *
 
13
 * Unless required by applicable law or agreed to in writing,
 
14
 * software distributed under the License is distributed on an
 
15
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
16
 * KIND, either express or implied.  See the License for the
 
17
 * specific language governing permissions and limitations
 
18
 * under the License.
 
19
 *
 
20
*/
 
21
 
 
22
describe('Whitelist API (cordova.whitelist)', function () {
 
23
        it("should exist", function() {
 
24
        expect(cordova.whitelist).toBeDefined();
 
25
        });
 
26
 
 
27
    describe("Match function", function() {
 
28
        function expectMatchWithResult(result) {
 
29
            return (function(url, patterns, description) {
 
30
                description = description || ((result ? "should accept " : "should reject ") + url + " for " + JSON.stringify(patterns));
 
31
                it(description, function() {
 
32
                    var cb = jasmine.createSpy();
 
33
                    runs(function() {
 
34
                        cordova.whitelist.match(url, patterns, cb);
 
35
                    });
 
36
                    waitsFor(function() { return cb.wasCalled; }, "success callback never called", Tests.TEST_TIMEOUT);
 
37
                    runs(function() {
 
38
                        expect(cb).toHaveBeenCalledWith(result);
 
39
                    });
 
40
                });
 
41
            });
 
42
        }
 
43
 
 
44
        var itShouldMatch = expectMatchWithResult(true);
 
45
        var itShouldNotMatch = expectMatchWithResult(false);
 
46
 
 
47
        it("should exist", function() {
 
48
            expect(cordova.whitelist.match).toBeDefined();
 
49
            expect(typeof cordova.whitelist.match).toBe("function");
 
50
        });
 
51
 
 
52
        itShouldMatch('http://www.apache.org/',['*'], "should accept any domain for *");
 
53
        itShouldNotMatch('http://www.apache.org/',[], "should not accept any domain for []");
 
54
 
 
55
        itShouldMatch('http://apache.org/', ['http://*.apache.org']);
 
56
        itShouldMatch('http://www.apache.org/', ['http://*.apache.org']);
 
57
        itShouldMatch('http://www.apache.org/some/path', ['http://*.apache.org']);
 
58
        itShouldMatch('http://some.domain.under.apache.org/', ['http://*.apache.org']);
 
59
        itShouldMatch('http://user:pass@apache.org/', ['http://*.apache.org']);
 
60
        itShouldMatch('http://user:pass@www.apache.org/', ['http://*.apache.org']);
 
61
        itShouldMatch('http://www.apache.org/?some=params', ['http://*.apache.org']);
 
62
        itShouldNotMatch('http://apache.com/', ['http://*.apache.org']);
 
63
        itShouldNotMatch('http://www.evil.com/?url=www.apache.org', ['http://*.apache.org']);
 
64
        itShouldNotMatch('http://www.evil.com/?url=http://www.apache.org', ['http://*.apache.org']);
 
65
        itShouldNotMatch('http://www.evil.com/?url=http%3A%2F%2Fwww%2Eapache%2Eorg', ['http://*.apache.org']);
 
66
        itShouldNotMatch('https://apache.org/', ['http://*.apache.org']);
 
67
        itShouldNotMatch('http://www.apache.org:pass@evil.com/', ['http://*.apache.org']);
 
68
        itShouldNotMatch('http://www.apache.org.evil.com/', ['http://*.apache.org']);
 
69
 
 
70
        itShouldMatch('http://www.apache.org/',['http://*.apache.org','https://*.apache.org']);
 
71
        itShouldMatch('https://www.apache.org/',['http://*.apache.org','https://*.apache.org']);
 
72
        itShouldNotMatch('ftp://www.apache.org/',['http://*.apache.org','https://*.apache.org']);
 
73
        itShouldNotMatch('http://www.apache.com/',['http://*.apache.org','https://*.apache.org']);
 
74
 
 
75
        itShouldMatch('http://www.apache.org/',['http://www.apache.org']);
 
76
        itShouldNotMatch('http://build.apache.org/',['http://www.apache.org']);
 
77
        itShouldNotMatch('http://apache.org/',['http://www.apache.org']);
 
78
 
 
79
        itShouldMatch('http://www.apache.org/', ['http://*/*']);
 
80
        itShouldMatch('http://www.apache.org/foo/bar.html', ['http://*/*']);
 
81
 
 
82
        itShouldMatch('http://www.apache.org/foo', ['http://*/foo*']);
 
83
        itShouldMatch('http://www.apache.org/foo/bar.html', ['http://*/foo*']);
 
84
        itShouldNotMatch('http://www.apache.org/', ['http://*/foo*']);
 
85
 
 
86
        itShouldMatch('file:///foo', ['file:///*']);
 
87
 
 
88
        itShouldMatch('file:///foo', ['file:///foo*']);
 
89
        itShouldMatch('file:///foo/bar.html', ['file:///foo*']);
 
90
        itShouldNotMatch('file:///etc/foo', ['file:///foo*']);
 
91
        itShouldNotMatch('http://www.apache.org/foo', ['file:///foo*']);
 
92
 
 
93
        itShouldMatch('http://www.apache.org/', ['*://www.apache.org/*']);
 
94
        itShouldMatch('https://www.apache.org/', ['*://www.apache.org/*']);
 
95
        itShouldMatch('ftp://www.apache.org/', ['*://www.apache.org/*']);
 
96
        itShouldMatch('file://www.apache.org/', ['*://www.apache.org/*']);
 
97
        itShouldMatch('content://www.apache.org/', ['*://www.apache.org/*']);
 
98
        itShouldMatch('foo://www.apache.org/', ['*://www.apache.org/*']);
 
99
        itShouldNotMatch('http://www.apache.com/', ['*://www.apache.org/*']);
 
100
 
 
101
        itShouldMatch('http://www.apache.org/', ['*.apache.org']);
 
102
        itShouldMatch('https://www.apache.org/', ['*.apache.org']);
 
103
        itShouldNotMatch('ftp://www.apache.org/', ['*.apache.org']);
 
104
 
 
105
        itShouldMatch('http://www.apache.org:81/', ['http://www.apache.org:81/*']);
 
106
        itShouldMatch('http://user:pass@www.apache.org:81/foo/bar.html', ['http://www.apache.org:81/*']);
 
107
        itShouldNotMatch('http://www.apache.org:80/', ['http://www.apache.org:81/*']);
 
108
        itShouldNotMatch('http://www.apache.org/', ['http://www.apache.org:81/*']);
 
109
        itShouldNotMatch('http://www.apache.org:foo/', ['http://www.apache.org:81/*']);
 
110
        itShouldNotMatch('http://www.apache.org:81@www.apache.org/', ['http://www.apache.org:81/*']);
 
111
        itShouldNotMatch('http://www.apache.org:81@www.evil.com/', ['http://www.apache.org:81/*']);
 
112
    });
 
113
 
 
114
    describe("Test function", function() {
 
115
        function expectTestWithResult(result) {
 
116
            return (function(url, description) {
 
117
                description = description || ((result ? "should accept " : "should reject ") + url);
 
118
                it(description, function() {
 
119
                    var cb = jasmine.createSpy();
 
120
                    runs(function() {
 
121
                        cordova.whitelist.test(url, cb);
 
122
                    });
 
123
                    waitsFor(function() { return cb.wasCalled; }, "success callback never called", Tests.TEST_TIMEOUT);
 
124
                    runs(function() {
 
125
                        expect(cb).toHaveBeenCalledWith(result);
 
126
                    });
 
127
                });
 
128
            });
 
129
        }
 
130
 
 
131
        var itShouldAccept = expectTestWithResult(true);
 
132
        var itShouldReject = expectTestWithResult(false);
 
133
 
 
134
        it("should exist", function() {
 
135
            expect(cordova.whitelist.test).toBeDefined();
 
136
            expect(typeof cordova.whitelist.test).toBe("function");
 
137
        });
 
138
 
 
139
        itShouldAccept('http://apache.org');
 
140
        itShouldAccept('http://apache.org/');
 
141
        itShouldAccept('http://www.apache.org/');
 
142
        itShouldAccept('http://www.apache.org/some/path');
 
143
        itShouldAccept('http://some.domain.under.apache.org/');
 
144
        itShouldAccept('http://user:pass@apache.org/');
 
145
        itShouldAccept('http://user:pass@www.apache.org/');
 
146
        itShouldAccept('https://www.apache.org/');
 
147
        itShouldReject('ftp://www.apache.org/');
 
148
        itShouldReject('http://www.apache.com/');
 
149
        itShouldReject('http://www.apache.org:pass@evil.com/');
 
150
        itShouldReject('http://www.apache.org.evil.com/');
 
151
        itShouldAccept('file:///foo');
 
152
        itShouldAccept('content:///foo');
 
153
    });
 
154
});