~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/app/javascript/testing/tests/test_mockio.js

Merged rocketfuel into trivial.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2011, Canonical Ltd. All rights reserved. */
 
2
 
 
3
YUI().use('test', 'console', 'node-event-simulate',
 
4
          'lp.testing.mockio', 'lp.testing.runner', function(Y) {
 
5
 
 
6
var suite = new Y.Test.Suite("lp.testing.mockio Tests");
 
7
 
 
8
var module = Y.lp.testing.mockio;
 
9
 
 
10
var make_call_recorder = function() {
 
11
    var recorder;
 
12
    recorder = function() {
 
13
        recorder.call_count += 1;
 
14
        recorder.args = arguments;
 
15
    };
 
16
    recorder.call_count = 0;
 
17
    recorder.args = null;
 
18
    return recorder;
 
19
};
 
20
 
 
21
suite.add(new Y.Test.Case({
 
22
    name: "lp.testing.mokio.MockIo",
 
23
 
 
24
    test_url: "https://launchpad.dev/test/url",
 
25
 
 
26
    setUp: function() {
 
27
        // Initialize call_count on recorders.
 
28
        this.test_config = {
 
29
            on: {
 
30
                start: make_call_recorder(),
 
31
                complete: make_call_recorder(),
 
32
                success: make_call_recorder(),
 
33
                failure: make_call_recorder()
 
34
            },
 
35
            context:  {marker: "context"},
 
36
            'arguments': ["arguments"]
 
37
        };
 
38
    },
 
39
 
 
40
    _make_mockio: function() {
 
41
        var mockio = new module.MockIo();
 
42
        mockio.io(this.test_url, this.test_config);
 
43
        return mockio;
 
44
    },
 
45
 
 
46
    test_respond_success: function() {
 
47
        // The success handler is called on success.
 
48
        var mockio = this._make_mockio();
 
49
        mockio.respond({status: 200});
 
50
        Y.Assert.areEqual(1, this.test_config.on.start.call_count);
 
51
        Y.Assert.areEqual(1, this.test_config.on.complete.call_count);
 
52
        Y.Assert.areEqual(1, this.test_config.on.success.call_count);
 
53
        Y.Assert.areEqual(0, this.test_config.on.failure.call_count);
 
54
    },
 
55
 
 
56
    test_respond_failure: function() {
 
57
        // The failure handler is called on failure.
 
58
        var mockio = this._make_mockio();
 
59
        mockio.respond({status: 500});
 
60
        Y.Assert.areEqual(1, this.test_config.on.start.call_count);
 
61
        Y.Assert.areEqual(1, this.test_config.on.complete.call_count);
 
62
        Y.Assert.areEqual(0, this.test_config.on.success.call_count);
 
63
        Y.Assert.areEqual(1, this.test_config.on.failure.call_count);
 
64
    },
 
65
 
 
66
    test_multiple_requests: function() {
 
67
        // Multiple requests are stored.
 
68
        var mockio = new module.MockIo();
 
69
        mockio.io(this.test_url, this.test_config);
 
70
        mockio.io(this.test_url, this.test_config);
 
71
        Y.Assert.areEqual(2, mockio.requests.length);
 
72
    },
 
73
 
 
74
    test_last_request: function() {
 
75
        // The last request is available through last_request.
 
76
        var mockio = new module.MockIo();
 
77
        mockio.io("Request 1", this.test_config);
 
78
        mockio.io("Request 2", this.test_config);
 
79
        Y.Assert.areEqual("Request 2", mockio.last_request.url);
 
80
    },
 
81
 
 
82
    test_status: function() {
 
83
        // The status is passed to the handler.
 
84
        var mockio = this._make_mockio();
 
85
        var expected_status = 503;
 
86
        mockio.respond({status: expected_status});
 
87
        Y.Assert.areEqual(
 
88
            expected_status, this.test_config.on.failure.args[1].status);
 
89
    },
 
90
 
 
91
    test_statusText: function() {
 
92
        // The statusText is passed to the handler.
 
93
        var mockio = this._make_mockio();
 
94
        var expected_status_text = "All is well";
 
95
        mockio.respond({statusText: expected_status_text});
 
96
        Y.Assert.areEqual(
 
97
            expected_status_text,
 
98
            this.test_config.on.success.args[1].statusText);
 
99
    },
 
100
 
 
101
    test_responseText: function() {
 
102
        // The responseText is passed to the handler.
 
103
        var mockio = this._make_mockio();
 
104
        var expected_response_text = "myresponse";
 
105
        mockio.respond({responseText: expected_response_text});
 
106
        Y.Assert.areEqual(
 
107
            expected_response_text,
 
108
            this.test_config.on.success.args[1].responseText);
 
109
    },
 
110
 
 
111
    test_responseHeader: function() {
 
112
        // A response header is passed to the handler.
 
113
        var mockio = this._make_mockio();
 
114
        var response = new Y.lp.testing.mockio.MockHttpResponse();
 
115
        var expected_header_key = "X-My-Header",
 
116
            expected_header_val = "MyHeaderValue",
 
117
            response_headers = {};
 
118
        response.setResponseHeader(expected_header_key,  expected_header_val);
 
119
        mockio.respond(response);
 
120
        var headers = this.test_config.on.success.args[1].responseHeaders;
 
121
        Y.Assert.areEqual(expected_header_val, headers[expected_header_key]);
 
122
    },
 
123
 
 
124
    test_success_helper: function() {
 
125
        // The success helper creates a successful response.
 
126
        var mockio = this._make_mockio(),
 
127
            response_text = "Success!";
 
128
        mockio.success({responseText: response_text});
 
129
        Y.Assert.areEqual(1, this.test_config.on.success.call_count);
 
130
        Y.Assert.areEqual(0, this.test_config.on.failure.call_count);
 
131
        Y.Assert.areEqual(
 
132
            response_text, mockio.last_request.response.responseText);
 
133
    },
 
134
 
 
135
    test_success_helper__own_status: function() {
 
136
        // The failure can define its own non-4xx or non-5xx status.
 
137
        var mockio = this._make_mockio(),
 
138
            status = 302;
 
139
        mockio.success({status: status});
 
140
        Y.Assert.areEqual(1, this.test_config.on.success.call_count);
 
141
        Y.Assert.areEqual(0, this.test_config.on.failure.call_count);
 
142
        Y.Assert.areEqual(status, mockio.last_request.response.status);
 
143
    },
 
144
 
 
145
    test_success_helper__status_override: function() {
 
146
        // A status that is 4xx or 5xx is overridden to be 200.
 
147
        // This is to guard against foot shooting.
 
148
        var mockio = this._make_mockio(),
 
149
            own_status = 500,
 
150
            real_status = 200;
 
151
        mockio.success({status: own_status});
 
152
        Y.Assert.areEqual(1, this.test_config.on.success.call_count);
 
153
        Y.Assert.areEqual(0, this.test_config.on.failure.call_count);
 
154
        Y.Assert.areEqual(real_status, mockio.last_request.response.status);
 
155
    },
 
156
 
 
157
    test_failure_helper: function() {
 
158
        // The failure helper creates a failed response.
 
159
        var mockio = this._make_mockio(),
 
160
            response_text = "Failure!";
 
161
        mockio.failure({responseText: response_text});
 
162
        Y.Assert.areEqual(0, this.test_config.on.success.call_count);
 
163
        Y.Assert.areEqual(1, this.test_config.on.failure.call_count);
 
164
        Y.Assert.areEqual(
 
165
            response_text, mockio.last_request.response.responseText);
 
166
    },
 
167
 
 
168
    test_failure_helper__own_status: function() {
 
169
        // The failure can define its own 4xx or 5xx status.
 
170
        var mockio = this._make_mockio(),
 
171
            status = 404;
 
172
        mockio.failure({status: status});
 
173
        Y.Assert.areEqual(0, this.test_config.on.success.call_count);
 
174
        Y.Assert.areEqual(1, this.test_config.on.failure.call_count);
 
175
        Y.Assert.areEqual(status, mockio.last_request.response.status);
 
176
    },
 
177
 
 
178
    test_failure_helper__status_override: function() {
 
179
        // A status that is not 4xx or 5xx is overridden to be 500.
 
180
        // This is to guard against foot shooting.
 
181
        var mockio = this._make_mockio(),
 
182
            own_status = 200,
 
183
            real_status = 500;
 
184
        mockio.failure({status: own_status});
 
185
        Y.Assert.areEqual(0, this.test_config.on.success.call_count);
 
186
        Y.Assert.areEqual(1, this.test_config.on.failure.call_count);
 
187
        Y.Assert.areEqual(real_status, mockio.last_request.response.status);
 
188
    }
 
189
}));
 
190
 
 
191
Y.lp.testing.Runner.run(suite);
 
192
 
 
193
});