1
/* Copyright (c) 2009, Canonical Ltd. All rights reserved. */
3
YUI.add('lazr.testing.mockio', function(Y) {
5
* A utility module for use in YUI unit-tests with a helper for mocking Y.io.
9
var MockIo = function() {
14
/* Save the Y.io() arguments. */
15
MockIo.prototype.io = function(uri, cfg) {
18
return this; // Usually this isn't used, except for logging.
21
/* Simulate the Xhr request/response cycle. */
22
MockIo.prototype.simulateXhr = function(response, is_failure) {
24
var context = cfg.context || this;
25
var args = cfg.arguments;
31
// See the Y.io utility documentation for the signatures.
33
cfg.on.start.call(context, tId, args);
35
if (cfg.on.complete) {
36
cfg.on.complete.call(context, tId, response, args);
38
if (cfg.on.success && !is_failure) {
39
cfg.on.success.call(context, tId, response, args);
41
if (cfg.on.failure && is_failure) {
42
cfg.on.failure.call(context, tId, response, args);
46
/* Make a successful XHR response object. */
47
MockIo.makeXhrSuccessResponse = function(responseText) {
48
var text = responseText || "";
56
/* Make a failed XHR response object. */
57
MockIo.makeXhrFailureResponse = function(responseText) {
58
var text = responseText || "";
61
statusText: "Internal Server Error",
66
Y.namespace("lazr.testing");
67
Y.lazr.testing.MockIo = MockIo;
1
/* Copyright (c) 2009-2011, Canonical Ltd. All rights reserved. */
3
YUI.add('lp.testing.mockio', function(Y) {
5
* A utility module for use in YUI unit-tests with a helper for
8
* @module lp.testing.mockio
10
var namespace = Y.namespace("lp.testing.mockio");
12
var MockHttpResponse = function(config) {
13
if (config === undefined) {
16
if (config.status !== undefined) {
17
this.status = config.status;
21
if (config.statusText !== undefined) {
22
this.statusText = config.statusText;
24
if (this.isFailure()) {
25
this.statusText = "Internal Server Error";
27
this.statusText = "OK";
30
if (config.responseText !== undefined) {
31
this.responseText = config.responseText;
33
this.responseText = '[]';
35
if (config.responseHeaders !== undefined) {
36
this.responseHeaders = config.responseHeaders;
38
this.responseHeaders = {};
42
MockHttpResponse.prototype = {
43
isFailure: function () {
44
return this.status >= 400;
47
setResponseHeader: function (header, value) {
48
this.responseHeaders[header] = value;
51
getResponseHeader: function(header) {
52
return this.responseHeaders[header];
55
namespace.MockHttpResponse = MockHttpResponse;
57
function MockHttpRequest(url, config){
63
/* Simulate the Xhr request/response cycle. */
64
MockHttpRequest.prototype.respond = function(response_config) {
65
var context = this.config.context || Y,
66
args = this.config['arguments'] || [],
68
response = this.response = new MockHttpResponse(response_config);
70
// See the Y.io utility documentation for the signatures.
71
if (this.config.on.start !== undefined) {
72
this.config.on.start.call(context, tId, args);
74
if (this.config.on.complete !== undefined) {
75
this.config.on.complete.call(context, tId, response, args);
77
if (this.config.on.success !== undefined && !response.isFailure()) {
78
this.config.on.success.call(context, tId, response, args);
80
if (this.config.on.failure !== undefined && response.isFailure()) {
81
this.config.on.failure.call(context, tId, response, args);
85
namespace.MockHttpRequest = MockHttpRequest;
87
var MockIo = function() {
89
this.last_request = null;
92
/* Save the Y.io() arguments. */
93
MockIo.prototype.io = function(url, config) {
94
this.last_request = new MockHttpRequest(url, config);
95
this.requests.push(this.last_request);
96
return this; // Usually this isn't used, except for logging.
99
/* Call respond method on last_request. */
100
MockIo.prototype.respond = function(response_config) {
101
this.last_request.respond(response_config);
104
/* Call respond method with successful values. */
105
MockIo.prototype.success = function(config) {
106
if (config === undefined) {
109
if (config.status === undefined || config.status >= 400) {
112
config.statusText = 'OK';
113
this.respond(config);
116
/* Call respond method with failed values. */
117
MockIo.prototype.failure = function(config) {
118
if (config === undefined) {
121
if (config.status === undefined || config.status < 400) {
124
config.statusText = 'Internal Server Error';
125
this.respond(config);
128
namespace.MockIo = MockIo;
130
/* Helper to select the io_provider. */
131
namespace.io_provider = function(mockio) {
132
if (mockio === undefined) {
138
/* Helper to select the io_provider from a config. */
139
namespace.io_provider_config = function(config, key) {
140
if (key === undefined) {
143
if (config === undefined || config[key] === undefined) {