|
115 | 115 | var self = this;
|
116 | 116 |
|
117 | 117 | this.fetch = function() {
|
118 |
| - self.clear(); |
| 118 | + self.code = null; |
| 119 | + self.response = null; |
| 120 | +
|
119 | 121 | $xhr(self.method, self.url, function(code, response) {
|
120 | 122 | self.code = code;
|
121 | 123 | self.response = response;
|
| 124 | + }, function(code, response) { |
| 125 | + self.code = code; |
| 126 | + self.response = response || "Request failed"; |
122 | 127 | });
|
123 | 128 | };
|
124 | 129 |
|
125 |
| - this.clear = function() { |
126 |
| - self.code = null; |
127 |
| - self.response = null; |
| 130 | + this.updateModel = function(method, url) { |
| 131 | + self.method = method; |
| 132 | + self.url = url; |
128 | 133 | };
|
129 | 134 | }
|
130 | 135 | FetchCntl.$inject = ['$xhr'];
|
|
134 | 139 | <option>GET</option>
|
135 | 140 | <option>JSON</option>
|
136 | 141 | </select>
|
137 |
| - <input type="text" name="url" value="index.html" size="80"/><br/> |
138 |
| - <button ng:click="fetch()">fetch</button> |
139 |
| - <button ng:click="clear()">clear</button> |
140 |
| - <a href="" ng:click="method='GET'; url='index.html'">sample</a> |
141 |
| - <a href="" ng:click="method='JSON'; url='https://www.googleapis.com/buzz/v1/activities/googlebuzz/@self?alt=json&callback=JSON_CALLBACK'">buzz</a> |
| 142 | + <input type="text" name="url" value="index.html" size="80"/> |
| 143 | + <button ng:click="fetch()">fetch</button><br> |
| 144 | + <button ng:click="updateModel('GET', 'index.html')">Sample GET</button> |
| 145 | + <button ng:click="updateModel('JSON', 'https://www.googleapis.com/buzz/v1/activities/googlebuzz/@self?alt=json&callback=JSON_CALLBACK')">Sample JSONP (Buzz API)</button> |
| 146 | + <button ng:click="updateModel('JSON', 'https://www.invalid_JSONP_request.com&callback=JSON_CALLBACK')">Invalid JSONP</button> |
142 | 147 | <pre>code={{code}}</pre>
|
143 | 148 | <pre>response={{response}}</pre>
|
144 | 149 | </div>
|
145 | 150 | </doc:source>
|
| 151 | + <doc:scenario> |
| 152 | + it('should make xhr GET request', function() { |
| 153 | + element(':button:contains("Sample GET")').click(); |
| 154 | + element(':button:contains("fetch")').click(); |
| 155 | + expect(binding('code')).toBe('code=200'); |
| 156 | + expect(binding('response')).toMatch(/angularjs.org/); |
| 157 | + }); |
| 158 | +
|
| 159 | + it('should make JSONP request to the Buzz API', function() { |
| 160 | + element(':button:contains("Buzz API")').click(); |
| 161 | + element(':button:contains("fetch")').click(); |
| 162 | + expect(binding('code')).toBe('code=200'); |
| 163 | + expect(binding('response')).toMatch(/buzz-feed/); |
| 164 | + }); |
| 165 | +
|
| 166 | + it('should make JSONP request to invalid URL and invoke the error handler', |
| 167 | + function() { |
| 168 | + element(':button:contains("Invalid JSONP")').click(); |
| 169 | + element(':button:contains("fetch")').click(); |
| 170 | + expect(binding('code')).toBe('code='); |
| 171 | + expect(binding('response')).toBe('response=Request failed'); |
| 172 | + }); |
| 173 | + </doc:scenario> |
146 | 174 | </doc:example>
|
147 | 175 | */
|
148 | 176 | angularServiceInject('$xhr', function($browser, $error, $log, $updateView){
|
|
0 commit comments