~launchpad-pqm/launchpad/devel

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
/* Copyright (c) 2009-2011, Canonical Ltd. All rights reserved. */

YUI.add('lp.testing.mockio', function(Y) {
    /**
     * A utility module for use in YUI unit-tests with a helper for
     * mocking Y.io.
     *
     * @module lp.testing.mockio
     */
    var namespace =  Y.namespace("lp.testing.mockio");

    var MockHttpResponse = function(config) {
        if (config === undefined) {
            config = {};
        }
        if (config.status !== undefined) {
            this.status = config.status;
        } else {
            this.status = 200;
        }
        if (config.statusText !== undefined) {
            this.statusText = config.statusText;
        } else {
            if (this.isFailure()) {
                this.statusText = "Internal Server Error";
            } else {
                this.statusText = "OK";
            }
        }
        if (config.responseText !== undefined) {
            this.responseText = config.responseText;
        } else {
            this.responseText = '[]';
        }
        if (config.responseHeaders !== undefined) {
            this.responseHeaders = config.responseHeaders;
        } else {
            this.responseHeaders = {};
        }
    };

    MockHttpResponse.prototype = {
        isFailure: function () {
            return this.status >= 400;
        },

        setResponseHeader: function (header, value) {
            this.responseHeaders[header] = value;
        },

        getResponseHeader: function(header) {
            return this.responseHeaders[header];
        }
    };
    namespace.MockHttpResponse = MockHttpResponse;

    function MockHttpRequest(url, config){
        this.url = url;
        this.config = config;
        this.response = null;
    }

    /* Simulate the Xhr request/response cycle. */
    MockHttpRequest.prototype.respond = function(response_config) {
        var context = this.config.context || Y,
            args = this.config['arguments'] || [],
            tId = 'mockTId',
            response = this.response = new MockHttpResponse(response_config);

        // See the Y.io utility documentation for the signatures.
        if (this.config.on.start !== undefined) {
            this.config.on.start.call(context, tId, args);
        }
        if (this.config.on.complete !== undefined) {
            this.config.on.complete.call(context, tId, response, args);
        }
        if (this.config.on.success !== undefined && !response.isFailure()) {
            this.config.on.success.call(context, tId, response, args);
        }
        if (this.config.on.failure !== undefined && response.isFailure()) {
            this.config.on.failure.call(context, tId, response, args);
        }
    };

    namespace.MockHttpRequest = MockHttpRequest;

    var MockIo = function() {
        this.requests = [];
        this.last_request = null;
    };

    /* Save the Y.io() arguments. */
    MockIo.prototype.io = function(url, config) {
        this.last_request = new MockHttpRequest(url, config);
        this.requests.push(this.last_request);
        return this;  // Usually this isn't used, except for logging.
    };

    /* Call respond method on last_request. */
    MockIo.prototype.respond = function(response_config) {
        this.last_request.respond(response_config);
    };

    /* Call respond method with successful values. */
    MockIo.prototype.success = function(config) {
        if (config === undefined) {
            config = {};
        }
        if (config.status === undefined || config.status >= 400) {
            config.status = 200;
        }
        config.statusText = 'OK';
        this.respond(config);
    };

    /* Call respond method with failed values. */
    MockIo.prototype.failure = function(config) {
        if (config === undefined) {
            config = {};
        }
        if (config.status === undefined || config.status < 400) {
            config.status = 500;
        }
        config.statusText = 'Internal Server Error';
        this.respond(config);
    };

    namespace.MockIo = MockIo;

    /* Helper to select the io_provider. */
    namespace.io_provider = function(mockio) {
        if (mockio === undefined) {
            return Y;
        }
        return mockio;
    };

    /* Helper to select the io_provider from a config. */
    namespace.io_provider_config = function(config, key) {
        if (key === undefined) {
            key = 'io_provider';
        }
        if (config === undefined || config[key] === undefined) {
            return Y;
        }
        return config[key];
    };

}, '0.1', {});