1
/* Copyright (c) 2011, Canonical Ltd. All rights reserved. */
3
YUI().use('test', 'console', 'node-event-simulate',
4
'lp.testing.mockio', 'lp.testing.runner', function(Y) {
6
var suite = new Y.Test.Suite("lp.testing.mockio Tests");
8
var module = Y.lp.testing.mockio;
10
var make_call_recorder = function() {
12
recorder = function() {
13
recorder.call_count += 1;
14
recorder.args = arguments;
16
recorder.call_count = 0;
21
suite.add(new Y.Test.Case({
22
name: "lp.testing.mokio.MockIo",
24
test_url: "https://launchpad.dev/test/url",
27
// Initialize call_count on recorders.
30
start: make_call_recorder(),
31
complete: make_call_recorder(),
32
success: make_call_recorder(),
33
failure: make_call_recorder()
35
context: {marker: "context"},
36
'arguments': ["arguments"]
40
_make_mockio: function() {
41
var mockio = new module.MockIo();
42
mockio.io(this.test_url, this.test_config);
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);
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);
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);
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);
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});
88
expected_status, this.test_config.on.failure.args[1].status);
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});
98
this.test_config.on.success.args[1].statusText);
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});
107
expected_response_text,
108
this.test_config.on.success.args[1].responseText);
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]);
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);
132
response_text, mockio.last_request.response.responseText);
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(),
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);
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(),
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);
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);
165
response_text, mockio.last_request.response.responseText);
168
test_failure_helper__own_status: function() {
169
// The failure can define its own 4xx or 5xx status.
170
var mockio = this._make_mockio(),
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);
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(),
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);
191
Y.lp.testing.Runner.run(suite);