@angular-wave/angular.ts 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +1 -0
- package/.eslintrc.cjs +29 -0
- package/.github/workflows/playwright.yml +27 -0
- package/CHANGELOG.md +17974 -0
- package/CODE_OF_CONDUCT.md +3 -0
- package/CONTRIBUTING.md +246 -0
- package/DEVELOPERS.md +488 -0
- package/LICENSE +22 -0
- package/Makefile +31 -0
- package/README.md +115 -0
- package/RELEASE.md +98 -0
- package/SECURITY.md +16 -0
- package/TRIAGING.md +135 -0
- package/css/angular.css +22 -0
- package/dist/angular-ts.cjs.js +36843 -0
- package/dist/angular-ts.esm.js +36841 -0
- package/dist/angular-ts.umd.js +36848 -0
- package/dist/build/angular-animate.js +4272 -0
- package/dist/build/angular-aria.js +426 -0
- package/dist/build/angular-message-format.js +1072 -0
- package/dist/build/angular-messages.js +829 -0
- package/dist/build/angular-mocks.js +3757 -0
- package/dist/build/angular-parse-ext.js +1275 -0
- package/dist/build/angular-resource.js +911 -0
- package/dist/build/angular-route.js +1266 -0
- package/dist/build/angular-sanitize.js +891 -0
- package/dist/build/angular-touch.js +368 -0
- package/dist/build/angular.js +36600 -0
- package/e2e/unit.spec.ts +15 -0
- package/images/android-chrome-192x192.png +0 -0
- package/images/android-chrome-512x512.png +0 -0
- package/images/apple-touch-icon.png +0 -0
- package/images/favicon-16x16.png +0 -0
- package/images/favicon-32x32.png +0 -0
- package/images/favicon.ico +0 -0
- package/images/site.webmanifest +1 -0
- package/index.html +104 -0
- package/package.json +47 -0
- package/playwright.config.ts +78 -0
- package/public/circle.html +1 -0
- package/public/my_child_directive.html +1 -0
- package/public/my_directive.html +1 -0
- package/public/my_other_directive.html +1 -0
- package/public/test.html +1 -0
- package/rollup.config.js +31 -0
- package/src/animations/animateCache.js +55 -0
- package/src/animations/animateChildrenDirective.js +105 -0
- package/src/animations/animateCss.js +1139 -0
- package/src/animations/animateCssDriver.js +291 -0
- package/src/animations/animateJs.js +367 -0
- package/src/animations/animateJsDriver.js +67 -0
- package/src/animations/animateQueue.js +851 -0
- package/src/animations/animation.js +506 -0
- package/src/animations/module.js +779 -0
- package/src/animations/ngAnimateSwap.js +119 -0
- package/src/animations/rafScheduler.js +50 -0
- package/src/animations/shared.js +378 -0
- package/src/constants.js +20 -0
- package/src/core/animate.js +845 -0
- package/src/core/animateCss.js +73 -0
- package/src/core/animateRunner.js +195 -0
- package/src/core/attributes.js +199 -0
- package/src/core/cache.js +45 -0
- package/src/core/compile.js +4727 -0
- package/src/core/controller.js +225 -0
- package/src/core/exceptionHandler.js +63 -0
- package/src/core/filter.js +146 -0
- package/src/core/interpolate.js +442 -0
- package/src/core/interval.js +188 -0
- package/src/core/intervalFactory.js +57 -0
- package/src/core/location.js +1086 -0
- package/src/core/parser/parse.js +2562 -0
- package/src/core/parser/parse.md +13 -0
- package/src/core/q.js +746 -0
- package/src/core/rootScope.js +1596 -0
- package/src/core/sanitizeUri.js +85 -0
- package/src/core/sce.js +1161 -0
- package/src/core/taskTrackerFactory.js +125 -0
- package/src/core/timeout.js +121 -0
- package/src/core/urlUtils.js +187 -0
- package/src/core/utils.js +1349 -0
- package/src/directive/a.js +37 -0
- package/src/directive/attrs.js +283 -0
- package/src/directive/bind.js +51 -0
- package/src/directive/bind.md +142 -0
- package/src/directive/change.js +12 -0
- package/src/directive/change.md +25 -0
- package/src/directive/cloak.js +12 -0
- package/src/directive/cloak.md +24 -0
- package/src/directive/events.js +75 -0
- package/src/directive/events.md +166 -0
- package/src/directive/form.js +725 -0
- package/src/directive/init.js +15 -0
- package/src/directive/init.md +41 -0
- package/src/directive/input.js +1783 -0
- package/src/directive/list.js +46 -0
- package/src/directive/list.md +22 -0
- package/src/directive/ngClass.js +249 -0
- package/src/directive/ngController.js +64 -0
- package/src/directive/ngCsp.js +82 -0
- package/src/directive/ngIf.js +134 -0
- package/src/directive/ngInclude.js +217 -0
- package/src/directive/ngModel.js +1356 -0
- package/src/directive/ngModelOptions.js +509 -0
- package/src/directive/ngOptions.js +670 -0
- package/src/directive/ngRef.js +90 -0
- package/src/directive/ngRepeat.js +650 -0
- package/src/directive/ngShowHide.js +255 -0
- package/src/directive/ngSwitch.js +178 -0
- package/src/directive/ngTransclude.js +98 -0
- package/src/directive/non-bindable.js +11 -0
- package/src/directive/non-bindable.md +17 -0
- package/src/directive/script.js +30 -0
- package/src/directive/select.js +624 -0
- package/src/directive/style.js +25 -0
- package/src/directive/style.md +23 -0
- package/src/directive/validators.js +329 -0
- package/src/exts/aria.js +544 -0
- package/src/exts/messages.js +852 -0
- package/src/filters/filter.js +207 -0
- package/src/filters/filter.md +69 -0
- package/src/filters/filters.js +239 -0
- package/src/filters/json.md +16 -0
- package/src/filters/limit-to.js +43 -0
- package/src/filters/limit-to.md +19 -0
- package/src/filters/order-by.js +183 -0
- package/src/filters/order-by.md +83 -0
- package/src/index.js +13 -0
- package/src/injector.js +1034 -0
- package/src/jqLite.js +1117 -0
- package/src/loader.js +1320 -0
- package/src/public.js +215 -0
- package/src/routeToRegExp.js +41 -0
- package/src/services/anchorScroll.js +135 -0
- package/src/services/browser.js +321 -0
- package/src/services/cacheFactory.js +398 -0
- package/src/services/cookieReader.js +72 -0
- package/src/services/document.js +64 -0
- package/src/services/http.js +1537 -0
- package/src/services/httpBackend.js +206 -0
- package/src/services/log.js +160 -0
- package/src/services/templateRequest.js +139 -0
- package/test/angular.spec.js +2153 -0
- package/test/aria/aria.spec.js +1245 -0
- package/test/binding.spec.js +504 -0
- package/test/build-test.html +14 -0
- package/test/injector.spec.js +2327 -0
- package/test/jasmine/jasmine-5.1.2/boot0.js +65 -0
- package/test/jasmine/jasmine-5.1.2/boot1.js +133 -0
- package/test/jasmine/jasmine-5.1.2/jasmine-html.js +963 -0
- package/test/jasmine/jasmine-5.1.2/jasmine.css +320 -0
- package/test/jasmine/jasmine-5.1.2/jasmine.js +10824 -0
- package/test/jasmine/jasmine-5.1.2/jasmine_favicon.png +0 -0
- package/test/jasmine/jasmine-browser.json +17 -0
- package/test/jasmine/jasmine.json +9 -0
- package/test/jqlite.spec.js +2133 -0
- package/test/loader.spec.js +219 -0
- package/test/messages/messages.spec.js +1146 -0
- package/test/min-err.spec.js +174 -0
- package/test/mock-test.html +13 -0
- package/test/module-test.html +15 -0
- package/test/ng/anomate.spec.js +606 -0
- package/test/ng/cache-factor.spec.js +334 -0
- package/test/ng/compile.spec.js +17956 -0
- package/test/ng/controller-provider.spec.js +227 -0
- package/test/ng/cookie-reader.spec.js +98 -0
- package/test/ng/directive/a.spec.js +192 -0
- package/test/ng/directive/bind.spec.js +334 -0
- package/test/ng/directive/boolean.spec.js +136 -0
- package/test/ng/directive/change.spec.js +71 -0
- package/test/ng/directive/class.spec.js +858 -0
- package/test/ng/directive/click.spec.js +38 -0
- package/test/ng/directive/cloak.spec.js +44 -0
- package/test/ng/directive/constoller.spec.js +194 -0
- package/test/ng/directive/element-style.spec.js +92 -0
- package/test/ng/directive/event.spec.js +282 -0
- package/test/ng/directive/form.spec.js +1518 -0
- package/test/ng/directive/href.spec.js +143 -0
- package/test/ng/directive/if.spec.js +402 -0
- package/test/ng/directive/include.spec.js +828 -0
- package/test/ng/directive/init.spec.js +68 -0
- package/test/ng/directive/input.spec.js +3810 -0
- package/test/ng/directive/list.spec.js +170 -0
- package/test/ng/directive/model-options.spec.js +1008 -0
- package/test/ng/directive/model.spec.js +1905 -0
- package/test/ng/directive/non-bindable.spec.js +55 -0
- package/test/ng/directive/options.spec.js +3583 -0
- package/test/ng/directive/ref.spec.js +575 -0
- package/test/ng/directive/repeat.spec.js +1675 -0
- package/test/ng/directive/script.spec.js +52 -0
- package/test/ng/directive/scrset.spec.js +67 -0
- package/test/ng/directive/select.spec.js +2541 -0
- package/test/ng/directive/show-hide.spec.js +253 -0
- package/test/ng/directive/src.spec.js +157 -0
- package/test/ng/directive/style.spec.js +178 -0
- package/test/ng/directive/switch.spec.js +647 -0
- package/test/ng/directive/validators.spec.js +717 -0
- package/test/ng/document.spec.js +52 -0
- package/test/ng/filter/filter.spec.js +714 -0
- package/test/ng/filter/filters.spec.js +35 -0
- package/test/ng/filter/limit-to.spec.js +251 -0
- package/test/ng/filter/order-by.spec.js +891 -0
- package/test/ng/filter.spec.js +149 -0
- package/test/ng/http-backend.spec.js +398 -0
- package/test/ng/http.spec.js +4071 -0
- package/test/ng/interpolate.spec.js +642 -0
- package/test/ng/interval.spec.js +343 -0
- package/test/ng/location.spec.js +3488 -0
- package/test/ng/on.spec.js +229 -0
- package/test/ng/parse.spec.js +4655 -0
- package/test/ng/prop.spec.js +805 -0
- package/test/ng/q.spec.js +2904 -0
- package/test/ng/root-element.spec.js +16 -0
- package/test/ng/sanitize-uri.spec.js +249 -0
- package/test/ng/sce.spec.js +660 -0
- package/test/ng/scope.spec.js +3442 -0
- package/test/ng/template-request.spec.js +236 -0
- package/test/ng/timeout.spec.js +351 -0
- package/test/ng/url-utils.spec.js +156 -0
- package/test/ng/utils.spec.js +144 -0
- package/test/original-test.html +21 -0
- package/test/public.spec.js +34 -0
- package/test/sanitize/bing-html.spec.js +36 -0
- package/test/server/express.js +158 -0
- package/test/test-utils.js +11 -0
- package/tsconfig.json +17 -0
- package/types/angular.d.ts +138 -0
- package/types/global.d.ts +9 -0
- package/types/index.d.ts +2357 -0
- package/types/jqlite.d.ts +558 -0
- package/vite.config.js +14 -0
|
@@ -0,0 +1,4071 @@
|
|
|
1
|
+
import { createInjector } from "../../src/injector";
|
|
2
|
+
import { isObject, isArray } from "../../src/core/utils";
|
|
3
|
+
import { publishExternalAPI } from "../../src/public";
|
|
4
|
+
|
|
5
|
+
export function getLastAjaxRequest() {
|
|
6
|
+
let ajaxEntries = performance
|
|
7
|
+
.getEntriesByType("resource")
|
|
8
|
+
.filter((entry) => entry.initiatorType === "xmlhttprequest");
|
|
9
|
+
return ajaxEntries[ajaxEntries.length - 1];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
describe("$http", function () {
|
|
13
|
+
let $http, $injector, $httpBackend, requests, response, $rootScope, $q;
|
|
14
|
+
|
|
15
|
+
beforeEach(function () {
|
|
16
|
+
publishExternalAPI();
|
|
17
|
+
requests = [];
|
|
18
|
+
$injector = createInjector(["ng"]);
|
|
19
|
+
$http = $injector.get("$http");
|
|
20
|
+
$httpBackend = $injector.get("$httpBackend");
|
|
21
|
+
$rootScope = $injector.get("$rootScope");
|
|
22
|
+
$q = $injector.get("$q");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("is a function", function () {
|
|
26
|
+
expect($http instanceof Function).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("returns a Promise", function () {
|
|
30
|
+
var result = $http({ url: "test" });
|
|
31
|
+
expect(result).toBeDefined();
|
|
32
|
+
expect(result.then).toBeDefined();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("makes an XMLHttpRequest to given URL", async function () {
|
|
36
|
+
await $http({
|
|
37
|
+
method: "POST",
|
|
38
|
+
url: "/mock/hello",
|
|
39
|
+
data: "hello",
|
|
40
|
+
}).then((res) => {
|
|
41
|
+
requests.push(res);
|
|
42
|
+
});
|
|
43
|
+
expect(requests.length).toBe(1);
|
|
44
|
+
expect(requests[0].config.method).toBe("POST");
|
|
45
|
+
expect(requests[0].config.url).toBe("/mock/hello");
|
|
46
|
+
expect(requests[0].config.data).toBe("hello");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("resolves promise when XHR result received", async function () {
|
|
50
|
+
await $http({
|
|
51
|
+
method: "GET",
|
|
52
|
+
url: "/mock/hello",
|
|
53
|
+
}).then(function (r) {
|
|
54
|
+
response = r;
|
|
55
|
+
});
|
|
56
|
+
expect(response).toBeDefined();
|
|
57
|
+
expect(response.status).toBe(200);
|
|
58
|
+
expect(response.statusText).toBe("OK");
|
|
59
|
+
expect(response.data).toBe("Hello");
|
|
60
|
+
expect(response.config.url).toEqual("/mock/hello");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("rejects promise when XHR result received with error status", async function () {
|
|
64
|
+
await $http({
|
|
65
|
+
method: "GET",
|
|
66
|
+
url: "/mock/401",
|
|
67
|
+
}).catch(function (r) {
|
|
68
|
+
response = r;
|
|
69
|
+
});
|
|
70
|
+
expect(response).toBeDefined();
|
|
71
|
+
expect(response.status).toBe(401);
|
|
72
|
+
expect(response.statusText).toBe("Unauthorized");
|
|
73
|
+
expect(response.data).toBe("Unauthorized");
|
|
74
|
+
expect(response.config.url).toEqual("/mock/401");
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("uses GET method by default", async function () {
|
|
78
|
+
await $http({
|
|
79
|
+
url: "/mock/hello",
|
|
80
|
+
}).then(function (r) {
|
|
81
|
+
response = r;
|
|
82
|
+
});
|
|
83
|
+
expect(response).toBeDefined();
|
|
84
|
+
expect(response.config.method).toBe("GET");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("sets deffault headers on request", async function () {
|
|
88
|
+
await $http({
|
|
89
|
+
url: "/mock/hello",
|
|
90
|
+
headers: {
|
|
91
|
+
Accept: "text/plain",
|
|
92
|
+
"Cache-Control": "no-cache",
|
|
93
|
+
},
|
|
94
|
+
}).then(function (r) {
|
|
95
|
+
response = r;
|
|
96
|
+
});
|
|
97
|
+
expect(response).toBeDefined();
|
|
98
|
+
|
|
99
|
+
expect(response.config.headers.Accept).toBe(
|
|
100
|
+
"application/json, text/plain, */*",
|
|
101
|
+
);
|
|
102
|
+
expect(response.config.headers["Cache-Control"]).toBe("no-cache");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("sets method-specific default headers on POST request", async function () {
|
|
106
|
+
await $http({
|
|
107
|
+
method: "POST",
|
|
108
|
+
url: "/mock/hello",
|
|
109
|
+
data: "42",
|
|
110
|
+
}).then(function (r) {
|
|
111
|
+
response = r;
|
|
112
|
+
});
|
|
113
|
+
expect(response).toBeDefined();
|
|
114
|
+
expect(response.config.headers["Content-Type"]).toBe(
|
|
115
|
+
"application/json;charset=utf-8",
|
|
116
|
+
);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("sets method-specific default headers on PUT request", async function () {
|
|
120
|
+
await $http({
|
|
121
|
+
method: "PUT",
|
|
122
|
+
url: "/mock/put",
|
|
123
|
+
data: "42",
|
|
124
|
+
}).then(function (r) {
|
|
125
|
+
response = r;
|
|
126
|
+
});
|
|
127
|
+
expect(response).toBeDefined();
|
|
128
|
+
expect(response.config.headers["Content-Type"]).toBe(
|
|
129
|
+
"application/json;charset=utf-8",
|
|
130
|
+
);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("sets method-specific default headers on PATCH request", async function () {
|
|
134
|
+
await $http({
|
|
135
|
+
method: "PATCH",
|
|
136
|
+
url: "/mock/patch",
|
|
137
|
+
data: "42",
|
|
138
|
+
}).then(function (r) {
|
|
139
|
+
response = r;
|
|
140
|
+
});
|
|
141
|
+
expect(response).toBeDefined();
|
|
142
|
+
expect(response.config.headers["Content-Type"]).toBe(
|
|
143
|
+
"application/json;charset=utf-8",
|
|
144
|
+
);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("exposes default headers for overriding", async function () {
|
|
148
|
+
$http.defaults.headers.post.Accept = "text/plain";
|
|
149
|
+
$http.defaults.headers.post["Content-Type"] = "text/plain;charset=utf-8";
|
|
150
|
+
await $http({
|
|
151
|
+
method: "POST",
|
|
152
|
+
url: "/mock/hello",
|
|
153
|
+
data: "42",
|
|
154
|
+
}).then(function (r) {
|
|
155
|
+
response = r;
|
|
156
|
+
});
|
|
157
|
+
expect(response).toBeDefined();
|
|
158
|
+
expect(response.config.headers["Accept"]).toBe("text/plain");
|
|
159
|
+
expect(response.config.headers["Content-Type"]).toBe(
|
|
160
|
+
"text/plain;charset=utf-8",
|
|
161
|
+
);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("merges default headers case-insensitively", async function () {
|
|
165
|
+
await $http({
|
|
166
|
+
method: "POST",
|
|
167
|
+
url: "/mock/hello",
|
|
168
|
+
data: "42",
|
|
169
|
+
headers: {
|
|
170
|
+
"content-type": "text/plain;charset=utf-8",
|
|
171
|
+
},
|
|
172
|
+
}).then(function (r) {
|
|
173
|
+
response = r;
|
|
174
|
+
});
|
|
175
|
+
expect(response).toBeDefined();
|
|
176
|
+
|
|
177
|
+
expect(response.config.headers["content-type"]).toBe(
|
|
178
|
+
"text/plain;charset=utf-8",
|
|
179
|
+
);
|
|
180
|
+
expect(response.config.headers["Content-Type"]).toBeUndefined();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("does not send content-type header when no data", async function () {
|
|
184
|
+
await $http({
|
|
185
|
+
method: "POST",
|
|
186
|
+
url: "/mock/hello",
|
|
187
|
+
headers: {
|
|
188
|
+
"Content-Type": "application/json;charset=utf-8",
|
|
189
|
+
},
|
|
190
|
+
}).then(function (r) {
|
|
191
|
+
response = r;
|
|
192
|
+
});
|
|
193
|
+
expect(response).toBeDefined();
|
|
194
|
+
expect(response.config.headers["Content-Type"]).not.toBe(
|
|
195
|
+
"application/json;charset=utf-8",
|
|
196
|
+
);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("supports functions as header values", async function () {
|
|
200
|
+
var contentTypeSpy = jasmine
|
|
201
|
+
.createSpy()
|
|
202
|
+
.and.returnValue("text/plain;charset=utf-8");
|
|
203
|
+
$http.defaults.headers.post["Content-Type"] = contentTypeSpy;
|
|
204
|
+
var request = {
|
|
205
|
+
method: "POST",
|
|
206
|
+
url: "/mock/hello",
|
|
207
|
+
data: 42,
|
|
208
|
+
};
|
|
209
|
+
await $http(request).then(function (r) {
|
|
210
|
+
response = r;
|
|
211
|
+
});
|
|
212
|
+
expect(contentTypeSpy).toHaveBeenCalledWith(request);
|
|
213
|
+
expect(response.config.headers["Content-Type"]).toBe(
|
|
214
|
+
"text/plain;charset=utf-8",
|
|
215
|
+
);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("ignores header function value when null/undefined", async function () {
|
|
219
|
+
var cacheControlSpy = jasmine.createSpy().and.returnValue(null);
|
|
220
|
+
$http.defaults.headers.post["Cache-Control"] = cacheControlSpy;
|
|
221
|
+
var request = {
|
|
222
|
+
method: "POST",
|
|
223
|
+
url: "/mock/hello",
|
|
224
|
+
data: 42,
|
|
225
|
+
};
|
|
226
|
+
await $http(request).then(function (r) {
|
|
227
|
+
response = r;
|
|
228
|
+
});
|
|
229
|
+
expect(cacheControlSpy).toHaveBeenCalledWith(request);
|
|
230
|
+
expect(response.config.headers["Cache-Control"]).toBeUndefined();
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("makes response headers available", async function () {
|
|
234
|
+
await $http({
|
|
235
|
+
method: "POST",
|
|
236
|
+
url: "/mock/hello",
|
|
237
|
+
data: 42,
|
|
238
|
+
}).then(function (r) {
|
|
239
|
+
response = r;
|
|
240
|
+
});
|
|
241
|
+
expect(response.headers).toBeDefined();
|
|
242
|
+
expect(response.headers instanceof Function).toBe(true);
|
|
243
|
+
expect(response.headers("Content-Type")).toBe(
|
|
244
|
+
"application/json; charset=utf-8",
|
|
245
|
+
);
|
|
246
|
+
expect(response.headers("content-type")).toBe(
|
|
247
|
+
"application/json; charset=utf-8",
|
|
248
|
+
);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("may returns all response headers", async function () {
|
|
252
|
+
await $http({
|
|
253
|
+
method: "POST",
|
|
254
|
+
url: "/mock/hello",
|
|
255
|
+
data: 42,
|
|
256
|
+
}).then(function (r) {
|
|
257
|
+
response = r;
|
|
258
|
+
});
|
|
259
|
+
expect(response.headers()).toBeTruthy();
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it("allows setting withCredentials", async function () {
|
|
263
|
+
await $http({
|
|
264
|
+
method: "POST",
|
|
265
|
+
url: "/mock/hello",
|
|
266
|
+
data: 42,
|
|
267
|
+
withCredentials: true,
|
|
268
|
+
}).then(function (r) {
|
|
269
|
+
response = r;
|
|
270
|
+
});
|
|
271
|
+
expect(response.config.withCredentials).toBe(true);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("allows setting withCredentials from defaults", async function () {
|
|
275
|
+
$http.defaults.withCredentials = true;
|
|
276
|
+
await $http({
|
|
277
|
+
method: "POST",
|
|
278
|
+
url: "/mock/hello",
|
|
279
|
+
data: 42,
|
|
280
|
+
}).then(function (r) {
|
|
281
|
+
response = r;
|
|
282
|
+
});
|
|
283
|
+
expect(response.config.withCredentials).toBe(true);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("allows transforming requests with functions", async function () {
|
|
287
|
+
var transformedData;
|
|
288
|
+
await $http({
|
|
289
|
+
method: "POST",
|
|
290
|
+
url: "/mock/hello",
|
|
291
|
+
data: 42,
|
|
292
|
+
transformRequest: function (data) {
|
|
293
|
+
transformedData = "*" + data + "*";
|
|
294
|
+
return transformedData;
|
|
295
|
+
},
|
|
296
|
+
}).then(function (r) {
|
|
297
|
+
response = r;
|
|
298
|
+
});
|
|
299
|
+
expect(transformedData).toBe("*42*");
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it("allows multiple request transform functions", async function () {
|
|
303
|
+
var transformedData;
|
|
304
|
+
await $http({
|
|
305
|
+
method: "POST",
|
|
306
|
+
url: "/mock/hello",
|
|
307
|
+
data: 42,
|
|
308
|
+
transformRequest: [
|
|
309
|
+
function (data) {
|
|
310
|
+
transformedData = "*" + data + "*";
|
|
311
|
+
return transformedData;
|
|
312
|
+
},
|
|
313
|
+
function (data) {
|
|
314
|
+
transformedData = "-" + data + "-";
|
|
315
|
+
return transformedData;
|
|
316
|
+
},
|
|
317
|
+
],
|
|
318
|
+
});
|
|
319
|
+
expect(transformedData).toBe("-*42*-");
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it("allows settings transforms in defaults", async function () {
|
|
323
|
+
var transformedData;
|
|
324
|
+
$http.defaults.transformRequest = [
|
|
325
|
+
function (data) {
|
|
326
|
+
transformedData = "*" + data + "*";
|
|
327
|
+
return transformedData;
|
|
328
|
+
},
|
|
329
|
+
];
|
|
330
|
+
await $http({
|
|
331
|
+
method: "POST",
|
|
332
|
+
url: "/mock/hello",
|
|
333
|
+
data: 42,
|
|
334
|
+
});
|
|
335
|
+
expect(transformedData).toBe("*42*");
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
it("passes request headers getter to transforms", async function () {
|
|
339
|
+
var transformedData;
|
|
340
|
+
$http.defaults.transformRequest = [
|
|
341
|
+
function (data, headers) {
|
|
342
|
+
if (headers("Content-Type") === "text/emphasized") {
|
|
343
|
+
transformedData = "*" + data + "*";
|
|
344
|
+
return transformedData;
|
|
345
|
+
} else {
|
|
346
|
+
return data;
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
];
|
|
350
|
+
await $http({
|
|
351
|
+
method: "POST",
|
|
352
|
+
url: "/mock/hello",
|
|
353
|
+
data: 42,
|
|
354
|
+
headers: {
|
|
355
|
+
"content-type": "text/emphasized",
|
|
356
|
+
},
|
|
357
|
+
});
|
|
358
|
+
expect(transformedData).toBe("*42*");
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it("allows transforming responses with functions", async function () {
|
|
362
|
+
await $http({
|
|
363
|
+
url: "/mock/hello",
|
|
364
|
+
transformResponse: function (data) {
|
|
365
|
+
return "*" + data + "*";
|
|
366
|
+
},
|
|
367
|
+
}).then(function (r) {
|
|
368
|
+
response = r;
|
|
369
|
+
});
|
|
370
|
+
$rootScope.$apply();
|
|
371
|
+
|
|
372
|
+
expect(response.data).toEqual("*Hello*");
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it("passes response headers to transform functions", async function () {
|
|
376
|
+
await $http({
|
|
377
|
+
url: "/mock/hello",
|
|
378
|
+
transformResponse: function (data) {
|
|
379
|
+
return "*" + data + "*";
|
|
380
|
+
},
|
|
381
|
+
}).then(function (r) {
|
|
382
|
+
response = r;
|
|
383
|
+
});
|
|
384
|
+
$rootScope.$apply();
|
|
385
|
+
|
|
386
|
+
expect(response.data).toEqual("*Hello*");
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
it("allows setting default response transforms", async function () {
|
|
390
|
+
$http.defaults.transformResponse = [
|
|
391
|
+
function (data) {
|
|
392
|
+
return "*" + data + "*";
|
|
393
|
+
},
|
|
394
|
+
];
|
|
395
|
+
await $http({
|
|
396
|
+
url: "/mock/hello",
|
|
397
|
+
}).then(function (r) {
|
|
398
|
+
response = r;
|
|
399
|
+
});
|
|
400
|
+
$rootScope.$apply();
|
|
401
|
+
|
|
402
|
+
expect(response.data).toEqual("*Hello*");
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
it("transforms error responses also", async function () {
|
|
406
|
+
await $http({
|
|
407
|
+
url: "/mock/401",
|
|
408
|
+
transformResponse: function (data) {
|
|
409
|
+
return "*" + data + "*";
|
|
410
|
+
},
|
|
411
|
+
}).catch(function (r) {
|
|
412
|
+
response = r;
|
|
413
|
+
});
|
|
414
|
+
$rootScope.$apply();
|
|
415
|
+
|
|
416
|
+
expect(response.data).toEqual("*Unauthorized*");
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
it("passes HTTP status to response transformers", async function () {
|
|
420
|
+
var response;
|
|
421
|
+
await $http({
|
|
422
|
+
url: "/mock/401",
|
|
423
|
+
transformResponse: function (data, headers, status) {
|
|
424
|
+
if (status === 401) {
|
|
425
|
+
return "unauthorized";
|
|
426
|
+
} else {
|
|
427
|
+
return data;
|
|
428
|
+
}
|
|
429
|
+
},
|
|
430
|
+
}).catch(function (r) {
|
|
431
|
+
response = r;
|
|
432
|
+
});
|
|
433
|
+
$rootScope.$apply();
|
|
434
|
+
|
|
435
|
+
expect(response.data).toEqual("unauthorized");
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
it("serializes object data to JSON for requests", async function () {
|
|
439
|
+
await $http({
|
|
440
|
+
method: "POST",
|
|
441
|
+
url: "/mock/post",
|
|
442
|
+
data: { aKey: 42 },
|
|
443
|
+
}).then(function (r) {
|
|
444
|
+
response = r;
|
|
445
|
+
});
|
|
446
|
+
$rootScope.$apply();
|
|
447
|
+
expect(response.data).toEqual({ aKey: 42 });
|
|
448
|
+
expect(response.config.data).toEqual({ aKey: 42 });
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it("serializes array data to JSON for requests", async function () {
|
|
452
|
+
await $http({
|
|
453
|
+
method: "POST",
|
|
454
|
+
url: "/mock/post",
|
|
455
|
+
data: [1, "two", 3],
|
|
456
|
+
}).then(function (r) {
|
|
457
|
+
response = r;
|
|
458
|
+
});
|
|
459
|
+
$rootScope.$apply();
|
|
460
|
+
expect(response.data).toEqual([1, "two", 3]);
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
it("does not serialize blobs for requests", async function () {
|
|
464
|
+
var blob;
|
|
465
|
+
if (window.Blob) {
|
|
466
|
+
blob = new Blob(["hello"]);
|
|
467
|
+
} else {
|
|
468
|
+
var BlobBuilder =
|
|
469
|
+
window.BlobBuilder ||
|
|
470
|
+
window.WebKitBlobBuilder ||
|
|
471
|
+
window.MozBlobBuilder ||
|
|
472
|
+
window.MSBlobBuilder;
|
|
473
|
+
var bb = new BlobBuilder();
|
|
474
|
+
bb.append("hello");
|
|
475
|
+
blob = bb.getBlob("text/plain");
|
|
476
|
+
}
|
|
477
|
+
await $http({
|
|
478
|
+
method: "POST",
|
|
479
|
+
url: "/mock/blob",
|
|
480
|
+
data: blob,
|
|
481
|
+
}).then(function (r) {
|
|
482
|
+
response = r;
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
$rootScope.$apply();
|
|
486
|
+
|
|
487
|
+
expect(response.config.data.size).toEqual(5);
|
|
488
|
+
expect(response.data).toEqual("");
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
it("does not serialize form data for requests", async function () {
|
|
492
|
+
var formData = new FormData();
|
|
493
|
+
formData.append("aField", "aValue");
|
|
494
|
+
await $http({
|
|
495
|
+
method: "POST",
|
|
496
|
+
url: "/mock/form",
|
|
497
|
+
data: formData,
|
|
498
|
+
}).then(function (r) {
|
|
499
|
+
response = r;
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
$rootScope.$apply();
|
|
503
|
+
|
|
504
|
+
expect(response.config.data).toEqual(formData);
|
|
505
|
+
expect(response.data).toEqual("");
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
it("parses JSON data for JSON responses", async function () {
|
|
509
|
+
await $http({
|
|
510
|
+
method: "POST",
|
|
511
|
+
url: "/mock/post",
|
|
512
|
+
data: { aKey: 42 },
|
|
513
|
+
}).then(function (r) {
|
|
514
|
+
response = r;
|
|
515
|
+
});
|
|
516
|
+
$rootScope.$apply();
|
|
517
|
+
|
|
518
|
+
expect(isObject(response.data)).toBe(true);
|
|
519
|
+
expect(response.data.aKey).toBe(42);
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
it("parses a JSON object response without content type", async function () {
|
|
523
|
+
await $http({
|
|
524
|
+
method: "POST",
|
|
525
|
+
url: "/mock/nocontent",
|
|
526
|
+
data: { aKey: 42 },
|
|
527
|
+
}).then(function (r) {
|
|
528
|
+
response = r;
|
|
529
|
+
});
|
|
530
|
+
$rootScope.$apply();
|
|
531
|
+
|
|
532
|
+
expect(isObject(response.data)).toBe(true);
|
|
533
|
+
expect(response.data.aKey).toBe(42);
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
it("parses a JSON array response without content type", async function () {
|
|
537
|
+
await $http({
|
|
538
|
+
method: "POST",
|
|
539
|
+
url: "/mock/nocontent",
|
|
540
|
+
data: [1, 2, 3],
|
|
541
|
+
}).then(function (r) {
|
|
542
|
+
response = r;
|
|
543
|
+
});
|
|
544
|
+
$rootScope.$apply();
|
|
545
|
+
|
|
546
|
+
expect(isArray(response.data)).toBe(true);
|
|
547
|
+
expect(response.data).toEqual([1, 2, 3]);
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
it("does not choke on response resembling JSON but not valid", async function () {
|
|
551
|
+
var response;
|
|
552
|
+
await $http({
|
|
553
|
+
method: "POST",
|
|
554
|
+
url: "/mock/invalidarray",
|
|
555
|
+
}).then(function (r) {
|
|
556
|
+
response = r;
|
|
557
|
+
});
|
|
558
|
+
$rootScope.$apply();
|
|
559
|
+
|
|
560
|
+
expect(response.data).toEqual("{1, 2, 3]");
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
it("does not try to parse interpolation expr as JSON", async function () {
|
|
564
|
+
var response;
|
|
565
|
+
await $http({
|
|
566
|
+
method: "GET",
|
|
567
|
+
url: "/mock/interpolation",
|
|
568
|
+
}).then(function (r) {
|
|
569
|
+
response = r;
|
|
570
|
+
});
|
|
571
|
+
$rootScope.$apply();
|
|
572
|
+
expect(response.data).toEqual("{{expr}}");
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
it("adds params to URL", async function () {
|
|
576
|
+
await $http({
|
|
577
|
+
method: "GET",
|
|
578
|
+
url: "/mock/hello",
|
|
579
|
+
params: {
|
|
580
|
+
a: 42,
|
|
581
|
+
},
|
|
582
|
+
}).then(function (r) {
|
|
583
|
+
response = r;
|
|
584
|
+
});
|
|
585
|
+
$rootScope.$apply();
|
|
586
|
+
expect(
|
|
587
|
+
response.config.paramSerializer(response.config.params).endsWith("a=42"),
|
|
588
|
+
).toBeTrue();
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
it("adds additional params to URL", async function () {
|
|
592
|
+
await $http({
|
|
593
|
+
url: "/mock/hello?a=42",
|
|
594
|
+
params: {
|
|
595
|
+
b: 42,
|
|
596
|
+
},
|
|
597
|
+
}).then(function (r) {
|
|
598
|
+
response = r;
|
|
599
|
+
});
|
|
600
|
+
$rootScope.$apply();
|
|
601
|
+
expect(
|
|
602
|
+
response.config.paramSerializer(response.config.params).endsWith("b=42"),
|
|
603
|
+
).toBeTrue();
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
it("escapes url characters in params", async function () {
|
|
607
|
+
await $http({
|
|
608
|
+
url: "/mock/hello",
|
|
609
|
+
params: {
|
|
610
|
+
"==": "&&",
|
|
611
|
+
},
|
|
612
|
+
}).then(function (r) {
|
|
613
|
+
response = r;
|
|
614
|
+
});
|
|
615
|
+
$rootScope.$apply();
|
|
616
|
+
|
|
617
|
+
expect(
|
|
618
|
+
response.config
|
|
619
|
+
.paramSerializer(response.config.params)
|
|
620
|
+
.endsWith("%3D%3D=%26%26"),
|
|
621
|
+
).toBeTrue();
|
|
622
|
+
});
|
|
623
|
+
|
|
624
|
+
it("does not attach null or undefined params", async function () {
|
|
625
|
+
await $http({
|
|
626
|
+
url: "/mock/hello",
|
|
627
|
+
params: {
|
|
628
|
+
a: null,
|
|
629
|
+
b: undefined,
|
|
630
|
+
},
|
|
631
|
+
}).then(function (r) {
|
|
632
|
+
response = r;
|
|
633
|
+
});
|
|
634
|
+
$rootScope.$apply();
|
|
635
|
+
expect(
|
|
636
|
+
response.config.paramSerializer(response.config.params).endsWith(""),
|
|
637
|
+
).toBeTrue();
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
it("attaches multiple params from arrays", async function () {
|
|
641
|
+
await $http({
|
|
642
|
+
url: "/mock/hello",
|
|
643
|
+
params: {
|
|
644
|
+
a: [42, 43],
|
|
645
|
+
},
|
|
646
|
+
}).then(function (r) {
|
|
647
|
+
response = r;
|
|
648
|
+
});
|
|
649
|
+
$rootScope.$apply();
|
|
650
|
+
expect(
|
|
651
|
+
response.config
|
|
652
|
+
.paramSerializer(response.config.params)
|
|
653
|
+
.endsWith("a=42&a=43"),
|
|
654
|
+
).toBeTrue();
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
it("serializes objects to json", async function () {
|
|
658
|
+
await $http({
|
|
659
|
+
method: "POST",
|
|
660
|
+
url: "/mock/post",
|
|
661
|
+
params: {
|
|
662
|
+
a: { b: 42 },
|
|
663
|
+
},
|
|
664
|
+
}).then(function (r) {
|
|
665
|
+
response = r;
|
|
666
|
+
});
|
|
667
|
+
$rootScope.$apply();
|
|
668
|
+
expect(
|
|
669
|
+
response.config
|
|
670
|
+
.paramSerializer(response.config.params)
|
|
671
|
+
.endsWith("a=%7B%22b%22:42%7D"),
|
|
672
|
+
).toBeTrue();
|
|
673
|
+
});
|
|
674
|
+
|
|
675
|
+
it("serializes dates to ISO strings", async function () {
|
|
676
|
+
await $http({
|
|
677
|
+
url: "/mock/hello",
|
|
678
|
+
params: {
|
|
679
|
+
a: new Date(2015, 0, 1, 12, 0, 0),
|
|
680
|
+
},
|
|
681
|
+
}).then(function (r) {
|
|
682
|
+
response = r;
|
|
683
|
+
});
|
|
684
|
+
$rootScope.$apply();
|
|
685
|
+
expect(
|
|
686
|
+
response.config
|
|
687
|
+
.paramSerializer(response.config.params)
|
|
688
|
+
.endsWith("00:00.000Z"),
|
|
689
|
+
).toBeTrue();
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
it("allows substituting param serializer", async function () {
|
|
693
|
+
await $http({
|
|
694
|
+
url: "/mock/hello",
|
|
695
|
+
params: {
|
|
696
|
+
a: 42,
|
|
697
|
+
b: 43,
|
|
698
|
+
},
|
|
699
|
+
paramSerializer: function (params) {
|
|
700
|
+
return Object.keys(params)
|
|
701
|
+
.map(function (k) {
|
|
702
|
+
return k + "=" + params[k] + "lol";
|
|
703
|
+
})
|
|
704
|
+
.join("&");
|
|
705
|
+
},
|
|
706
|
+
}).then(function (r) {
|
|
707
|
+
response = r;
|
|
708
|
+
});
|
|
709
|
+
$rootScope.$apply();
|
|
710
|
+
|
|
711
|
+
expect(
|
|
712
|
+
response.config
|
|
713
|
+
.paramSerializer(response.config.params)
|
|
714
|
+
.endsWith("a=42lol&b=43lol"),
|
|
715
|
+
).toBeTrue();
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
it("allows substituting param serializer through DI", async function () {
|
|
719
|
+
var injector = createInjector([
|
|
720
|
+
"ng",
|
|
721
|
+
function ($provide) {
|
|
722
|
+
$provide.factory("mySpecialSerializer", function () {
|
|
723
|
+
return function (params) {
|
|
724
|
+
return Object.keys(params)
|
|
725
|
+
.map(function (k) {
|
|
726
|
+
return k + "=" + params[k] + "lol";
|
|
727
|
+
})
|
|
728
|
+
.join("&");
|
|
729
|
+
};
|
|
730
|
+
});
|
|
731
|
+
},
|
|
732
|
+
]);
|
|
733
|
+
|
|
734
|
+
await injector.invoke(async function ($http, $rootScope) {
|
|
735
|
+
await $http({
|
|
736
|
+
url: "/mock/hello",
|
|
737
|
+
params: {
|
|
738
|
+
a: 42,
|
|
739
|
+
b: 43,
|
|
740
|
+
},
|
|
741
|
+
paramSerializer: "mySpecialSerializer",
|
|
742
|
+
}).then(function (r) {
|
|
743
|
+
response = r;
|
|
744
|
+
});
|
|
745
|
+
$rootScope.$apply();
|
|
746
|
+
});
|
|
747
|
+
expect(
|
|
748
|
+
response.config
|
|
749
|
+
.paramSerializer(response.config.params)
|
|
750
|
+
.endsWith("a=42lol&b=43lol"),
|
|
751
|
+
).toBeTrue();
|
|
752
|
+
});
|
|
753
|
+
|
|
754
|
+
it("makes default param serializer available through DI", async function () {
|
|
755
|
+
var injector = createInjector(["ng"]);
|
|
756
|
+
injector.invoke(function ($httpParamSerializer) {
|
|
757
|
+
var result = $httpParamSerializer({ a: 42, b: 43 });
|
|
758
|
+
expect(result).toEqual("a=42&b=43");
|
|
759
|
+
});
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
it("supports shorthand method for GET", async function () {
|
|
763
|
+
await $http
|
|
764
|
+
.get("/mock/hello", {
|
|
765
|
+
params: { q: 42 },
|
|
766
|
+
})
|
|
767
|
+
.then(function (r) {
|
|
768
|
+
response = r;
|
|
769
|
+
});
|
|
770
|
+
$rootScope.$apply();
|
|
771
|
+
|
|
772
|
+
expect(
|
|
773
|
+
response.config.paramSerializer(response.config.params).endsWith("q=42"),
|
|
774
|
+
).toBeTrue();
|
|
775
|
+
expect(response.config.method).toBe("GET");
|
|
776
|
+
});
|
|
777
|
+
|
|
778
|
+
it("supports shorthand method for HEAD", async function () {
|
|
779
|
+
await $http
|
|
780
|
+
.head("/mock/head", {
|
|
781
|
+
params: { q: 42 },
|
|
782
|
+
})
|
|
783
|
+
.then(function (r) {
|
|
784
|
+
response = r;
|
|
785
|
+
});
|
|
786
|
+
$rootScope.$apply();
|
|
787
|
+
|
|
788
|
+
expect(
|
|
789
|
+
response.config.paramSerializer(response.config.params).endsWith("q=42"),
|
|
790
|
+
).toBeTrue();
|
|
791
|
+
expect(response.config.method).toBe("HEAD");
|
|
792
|
+
});
|
|
793
|
+
|
|
794
|
+
it("supports shorthand method for DELETE", async function () {
|
|
795
|
+
await $http
|
|
796
|
+
.delete("/mock/delete", {
|
|
797
|
+
params: { q: 42 },
|
|
798
|
+
})
|
|
799
|
+
.then(function (r) {
|
|
800
|
+
response = r;
|
|
801
|
+
});
|
|
802
|
+
|
|
803
|
+
$rootScope.$apply();
|
|
804
|
+
|
|
805
|
+
expect(
|
|
806
|
+
response.config.paramSerializer(response.config.params).endsWith("q=42"),
|
|
807
|
+
).toBeTrue();
|
|
808
|
+
expect(response.config.method).toBe("DELETE");
|
|
809
|
+
});
|
|
810
|
+
|
|
811
|
+
it("supports shorthand method for POST with data", async function () {
|
|
812
|
+
await $http
|
|
813
|
+
.post("/mock/hello", "data", {
|
|
814
|
+
params: { q: 42 },
|
|
815
|
+
})
|
|
816
|
+
.then(function (r) {
|
|
817
|
+
response = r;
|
|
818
|
+
});
|
|
819
|
+
$rootScope.$apply();
|
|
820
|
+
|
|
821
|
+
expect(
|
|
822
|
+
response.config.paramSerializer(response.config.params).endsWith("q=42"),
|
|
823
|
+
).toBeTrue();
|
|
824
|
+
expect(response.config.method).toBe("POST");
|
|
825
|
+
expect(response.config.data).toBe("data");
|
|
826
|
+
});
|
|
827
|
+
|
|
828
|
+
it("supports shorthand method for PUT with data", async function () {
|
|
829
|
+
await $http
|
|
830
|
+
.put("/mock/put", "data", {
|
|
831
|
+
params: { q: 42 },
|
|
832
|
+
})
|
|
833
|
+
.then(function (r) {
|
|
834
|
+
response = r;
|
|
835
|
+
});
|
|
836
|
+
$rootScope.$apply();
|
|
837
|
+
expect(
|
|
838
|
+
response.config.paramSerializer(response.config.params).endsWith("q=42"),
|
|
839
|
+
).toBeTrue();
|
|
840
|
+
expect(response.config.method).toBe("PUT");
|
|
841
|
+
expect(response.config.data).toBe("data");
|
|
842
|
+
});
|
|
843
|
+
|
|
844
|
+
it("supports shorthand method for PATCH with data", async function () {
|
|
845
|
+
await $http
|
|
846
|
+
.patch("/mock/patch", "data", {
|
|
847
|
+
params: { q: 42 },
|
|
848
|
+
})
|
|
849
|
+
.then(function (r) {
|
|
850
|
+
response = r;
|
|
851
|
+
});
|
|
852
|
+
$rootScope.$apply();
|
|
853
|
+
|
|
854
|
+
expect(
|
|
855
|
+
response.config.paramSerializer(response.config.params).endsWith("q=42"),
|
|
856
|
+
).toBeTrue();
|
|
857
|
+
expect(response.config.method).toBe("PATCH");
|
|
858
|
+
expect(response.config.data).toBe("data");
|
|
859
|
+
});
|
|
860
|
+
|
|
861
|
+
it("allows attaching interceptor factories", async function () {
|
|
862
|
+
var interceptorFactorySpy = jasmine.createSpy();
|
|
863
|
+
var injector = createInjector([
|
|
864
|
+
"ng",
|
|
865
|
+
function ($httpProvider) {
|
|
866
|
+
$httpProvider.interceptors.push(interceptorFactorySpy);
|
|
867
|
+
},
|
|
868
|
+
]);
|
|
869
|
+
$http = injector.get("$http");
|
|
870
|
+
|
|
871
|
+
expect(interceptorFactorySpy).toHaveBeenCalled();
|
|
872
|
+
});
|
|
873
|
+
|
|
874
|
+
it("uses DI to instantiate interceptors", async function () {
|
|
875
|
+
var interceptorFactorySpy = jasmine.createSpy();
|
|
876
|
+
var injector = createInjector([
|
|
877
|
+
"ng",
|
|
878
|
+
function ($httpProvider) {
|
|
879
|
+
$httpProvider.interceptors.push(["$rootScope", interceptorFactorySpy]);
|
|
880
|
+
},
|
|
881
|
+
]);
|
|
882
|
+
$http = injector.get("$http");
|
|
883
|
+
var $rootScope = injector.get("$rootScope");
|
|
884
|
+
|
|
885
|
+
expect(interceptorFactorySpy).toHaveBeenCalledWith($rootScope);
|
|
886
|
+
});
|
|
887
|
+
|
|
888
|
+
it("allows referencing existing interceptor factories", async function () {
|
|
889
|
+
var interceptorFactorySpy = jasmine.createSpy().and.returnValue({});
|
|
890
|
+
var injector = createInjector([
|
|
891
|
+
"ng",
|
|
892
|
+
function ($provide, $httpProvider) {
|
|
893
|
+
$provide.factory("myInterceptor", interceptorFactorySpy);
|
|
894
|
+
$httpProvider.interceptors.push("myInterceptor");
|
|
895
|
+
},
|
|
896
|
+
]);
|
|
897
|
+
$http = injector.get("$http");
|
|
898
|
+
|
|
899
|
+
expect(interceptorFactorySpy).toHaveBeenCalled();
|
|
900
|
+
});
|
|
901
|
+
|
|
902
|
+
it("allows intercepting requests", async function () {
|
|
903
|
+
var injector = createInjector([
|
|
904
|
+
"ng",
|
|
905
|
+
function ($httpProvider) {
|
|
906
|
+
$httpProvider.interceptors.push(function () {
|
|
907
|
+
return {
|
|
908
|
+
request: function (config) {
|
|
909
|
+
config.params.intercepted = true;
|
|
910
|
+
return config;
|
|
911
|
+
},
|
|
912
|
+
};
|
|
913
|
+
});
|
|
914
|
+
},
|
|
915
|
+
]);
|
|
916
|
+
$http = injector.get("$http");
|
|
917
|
+
$rootScope = injector.get("$rootScope");
|
|
918
|
+
|
|
919
|
+
await $http.get("/mock/hello", { params: {} }).then(function (r) {
|
|
920
|
+
response = r;
|
|
921
|
+
});
|
|
922
|
+
|
|
923
|
+
$rootScope.$apply();
|
|
924
|
+
|
|
925
|
+
expect(
|
|
926
|
+
response.config
|
|
927
|
+
.paramSerializer(response.config.params)
|
|
928
|
+
.endsWith("intercepted=true"),
|
|
929
|
+
).toBeTrue();
|
|
930
|
+
});
|
|
931
|
+
|
|
932
|
+
it("allows returning promises from request intercepts", async function () {
|
|
933
|
+
var injector = createInjector([
|
|
934
|
+
"ng",
|
|
935
|
+
function ($httpProvider) {
|
|
936
|
+
$httpProvider.interceptors.push(function ($q) {
|
|
937
|
+
return {
|
|
938
|
+
request: function (config) {
|
|
939
|
+
config.params.intercepted = true;
|
|
940
|
+
return $q.when(config);
|
|
941
|
+
},
|
|
942
|
+
};
|
|
943
|
+
});
|
|
944
|
+
},
|
|
945
|
+
]);
|
|
946
|
+
$http = injector.get("$http");
|
|
947
|
+
$rootScope = injector.get("$rootScope");
|
|
948
|
+
|
|
949
|
+
await $http.get("/mock/hello", { params: {} }).then(function (r) {
|
|
950
|
+
response = r;
|
|
951
|
+
});
|
|
952
|
+
$rootScope.$apply();
|
|
953
|
+
|
|
954
|
+
expect(
|
|
955
|
+
response.config
|
|
956
|
+
.paramSerializer(response.config.params)
|
|
957
|
+
.endsWith("intercepted=true"),
|
|
958
|
+
).toBeTrue();
|
|
959
|
+
});
|
|
960
|
+
|
|
961
|
+
it("allows intercepting responses", async function () {
|
|
962
|
+
var injector = createInjector([
|
|
963
|
+
"ng",
|
|
964
|
+
function ($httpProvider) {
|
|
965
|
+
$httpProvider.interceptors.push(() => ({
|
|
966
|
+
response: function (response) {
|
|
967
|
+
response.intercepted = true;
|
|
968
|
+
return response;
|
|
969
|
+
},
|
|
970
|
+
}));
|
|
971
|
+
},
|
|
972
|
+
]);
|
|
973
|
+
$http = injector.get("$http");
|
|
974
|
+
$rootScope = injector.get("$rootScope");
|
|
975
|
+
|
|
976
|
+
var response;
|
|
977
|
+
await $http.get("/mock/hello").then(function (r) {
|
|
978
|
+
response = r;
|
|
979
|
+
});
|
|
980
|
+
$rootScope.$apply();
|
|
981
|
+
|
|
982
|
+
expect(response.intercepted).toBe(true);
|
|
983
|
+
});
|
|
984
|
+
|
|
985
|
+
it("allows intercepting request errors", async function () {
|
|
986
|
+
var requestErrorSpy = jasmine.createSpy();
|
|
987
|
+
var injector = createInjector([
|
|
988
|
+
"ng",
|
|
989
|
+
function ($httpProvider) {
|
|
990
|
+
$httpProvider.interceptors.push(() => ({
|
|
991
|
+
request: function (config) {
|
|
992
|
+
throw "fail";
|
|
993
|
+
},
|
|
994
|
+
}));
|
|
995
|
+
$httpProvider.interceptors.push(() => {
|
|
996
|
+
return {
|
|
997
|
+
requestError: requestErrorSpy,
|
|
998
|
+
};
|
|
999
|
+
});
|
|
1000
|
+
},
|
|
1001
|
+
]);
|
|
1002
|
+
$http = injector.get("$http");
|
|
1003
|
+
$rootScope = injector.get("$rootScope");
|
|
1004
|
+
|
|
1005
|
+
try {
|
|
1006
|
+
await $http.get("/mock/hello").then(function (r) {
|
|
1007
|
+
response = r;
|
|
1008
|
+
});
|
|
1009
|
+
} catch {}
|
|
1010
|
+
|
|
1011
|
+
expect(requestErrorSpy).toHaveBeenCalledWith("fail");
|
|
1012
|
+
});
|
|
1013
|
+
|
|
1014
|
+
it("allows intercepting response errors", async function () {
|
|
1015
|
+
var responseErrorSpy = jasmine.createSpy();
|
|
1016
|
+
var injector = createInjector([
|
|
1017
|
+
"ng",
|
|
1018
|
+
function ($httpProvider) {
|
|
1019
|
+
$httpProvider.interceptors.push(() => ({
|
|
1020
|
+
responseError: responseErrorSpy,
|
|
1021
|
+
}));
|
|
1022
|
+
$httpProvider.interceptors.push(() => ({
|
|
1023
|
+
response: function () {
|
|
1024
|
+
throw "fail";
|
|
1025
|
+
},
|
|
1026
|
+
}));
|
|
1027
|
+
},
|
|
1028
|
+
]);
|
|
1029
|
+
$http = injector.get("$http");
|
|
1030
|
+
$rootScope = injector.get("$rootScope");
|
|
1031
|
+
|
|
1032
|
+
await $http.get("/mock/hello");
|
|
1033
|
+
$rootScope.$apply();
|
|
1034
|
+
$rootScope.$apply();
|
|
1035
|
+
|
|
1036
|
+
expect(responseErrorSpy).toHaveBeenCalledWith("fail");
|
|
1037
|
+
});
|
|
1038
|
+
|
|
1039
|
+
it("allows attaching success handlers", async function () {
|
|
1040
|
+
var data, status, config;
|
|
1041
|
+
await $http.get("/mock/hello").then(function (res) {
|
|
1042
|
+
data = res.data;
|
|
1043
|
+
status = res.status;
|
|
1044
|
+
config = res.config;
|
|
1045
|
+
});
|
|
1046
|
+
$rootScope.$apply();
|
|
1047
|
+
|
|
1048
|
+
expect(data).toBe("Hello");
|
|
1049
|
+
expect(status).toBe(200);
|
|
1050
|
+
expect(config.method).toBe("GET");
|
|
1051
|
+
});
|
|
1052
|
+
|
|
1053
|
+
it("allows attaching error handlers", async function () {
|
|
1054
|
+
var res;
|
|
1055
|
+
await $http.get("/mock/401").then(
|
|
1056
|
+
() => {},
|
|
1057
|
+
function (r) {
|
|
1058
|
+
res = r;
|
|
1059
|
+
},
|
|
1060
|
+
);
|
|
1061
|
+
$rootScope.$apply();
|
|
1062
|
+
|
|
1063
|
+
expect(res.data).toBe("Unauthorized");
|
|
1064
|
+
expect(res.status).toBe(401);
|
|
1065
|
+
expect(res.config.method).toBe("GET");
|
|
1066
|
+
});
|
|
1067
|
+
|
|
1068
|
+
describe("JQ-like param serialization", function () {
|
|
1069
|
+
it("is possible", async function () {
|
|
1070
|
+
await $http({
|
|
1071
|
+
url: "/mock/hello",
|
|
1072
|
+
params: {
|
|
1073
|
+
a: 42,
|
|
1074
|
+
b: 43,
|
|
1075
|
+
},
|
|
1076
|
+
paramSerializer: "$httpParamSerializerJQLike",
|
|
1077
|
+
}).then(function (r) {
|
|
1078
|
+
response = r;
|
|
1079
|
+
});
|
|
1080
|
+
$rootScope.$apply();
|
|
1081
|
+
|
|
1082
|
+
expect(
|
|
1083
|
+
response.config
|
|
1084
|
+
.paramSerializer(response.config.params)
|
|
1085
|
+
.endsWith("a=42&b=43"),
|
|
1086
|
+
).toBeTrue();
|
|
1087
|
+
});
|
|
1088
|
+
|
|
1089
|
+
it("serializes null and undefined elements as empty", async function () {
|
|
1090
|
+
await $http({
|
|
1091
|
+
url: "/mock/hello",
|
|
1092
|
+
params: {
|
|
1093
|
+
a: null,
|
|
1094
|
+
b: undefined,
|
|
1095
|
+
},
|
|
1096
|
+
paramSerializer: "$httpParamSerializerJQLike",
|
|
1097
|
+
}).then(function (r) {
|
|
1098
|
+
response = r;
|
|
1099
|
+
});
|
|
1100
|
+
$rootScope.$apply();
|
|
1101
|
+
|
|
1102
|
+
expect(
|
|
1103
|
+
response.config
|
|
1104
|
+
.paramSerializer(response.config.params)
|
|
1105
|
+
.endsWith("a=&b="),
|
|
1106
|
+
).toBeTrue();
|
|
1107
|
+
});
|
|
1108
|
+
|
|
1109
|
+
it("uses square brackets in arrays", async function () {
|
|
1110
|
+
await $http({
|
|
1111
|
+
url: "/mock/hello",
|
|
1112
|
+
params: {
|
|
1113
|
+
a: [42, 43],
|
|
1114
|
+
},
|
|
1115
|
+
paramSerializer: "$httpParamSerializerJQLike",
|
|
1116
|
+
}).then(function (r) {
|
|
1117
|
+
response = r;
|
|
1118
|
+
});
|
|
1119
|
+
$rootScope.$apply();
|
|
1120
|
+
|
|
1121
|
+
expect(
|
|
1122
|
+
response.config
|
|
1123
|
+
.paramSerializer(response.config.params)
|
|
1124
|
+
.endsWith("a%5B%5D=42&a%5B%5D=43"),
|
|
1125
|
+
).toBeTrue();
|
|
1126
|
+
});
|
|
1127
|
+
|
|
1128
|
+
it("uses square brackets in objects", async function () {
|
|
1129
|
+
await $http({
|
|
1130
|
+
url: "/mock/hello",
|
|
1131
|
+
params: {
|
|
1132
|
+
a: { b: 42, c: 43 },
|
|
1133
|
+
},
|
|
1134
|
+
paramSerializer: "$httpParamSerializerJQLike",
|
|
1135
|
+
}).then(function (r) {
|
|
1136
|
+
response = r;
|
|
1137
|
+
});
|
|
1138
|
+
$rootScope.$apply();
|
|
1139
|
+
|
|
1140
|
+
expect(
|
|
1141
|
+
response.config
|
|
1142
|
+
.paramSerializer(response.config.params)
|
|
1143
|
+
.endsWith("a%5Bb%5D=42&a%5Bc%5D=43"),
|
|
1144
|
+
).toBeTrue();
|
|
1145
|
+
});
|
|
1146
|
+
|
|
1147
|
+
it("supports nesting in objects", async function () {
|
|
1148
|
+
await $http({
|
|
1149
|
+
url: "/mock/hello",
|
|
1150
|
+
params: {
|
|
1151
|
+
a: { b: { c: 42 } },
|
|
1152
|
+
},
|
|
1153
|
+
paramSerializer: "$httpParamSerializerJQLike",
|
|
1154
|
+
}).then(function (r) {
|
|
1155
|
+
response = r;
|
|
1156
|
+
});
|
|
1157
|
+
$rootScope.$apply();
|
|
1158
|
+
|
|
1159
|
+
expect(
|
|
1160
|
+
response.config
|
|
1161
|
+
.paramSerializer(response.config.params)
|
|
1162
|
+
.endsWith("a%5Bb%5D%5Bc%5D=42"),
|
|
1163
|
+
).toBeTrue();
|
|
1164
|
+
});
|
|
1165
|
+
|
|
1166
|
+
it("appends array indexes when items are objects", async function () {
|
|
1167
|
+
await $http({
|
|
1168
|
+
url: "/mock/hello",
|
|
1169
|
+
params: {
|
|
1170
|
+
a: [{ b: 42 }],
|
|
1171
|
+
},
|
|
1172
|
+
paramSerializer: "$httpParamSerializerJQLike",
|
|
1173
|
+
}).then(function (r) {
|
|
1174
|
+
response = r;
|
|
1175
|
+
});
|
|
1176
|
+
$rootScope.$apply();
|
|
1177
|
+
|
|
1178
|
+
expect(
|
|
1179
|
+
response.config
|
|
1180
|
+
.paramSerializer(response.config.params)
|
|
1181
|
+
.endsWith("a%5B0%5D%5Bb%5D=42"),
|
|
1182
|
+
).toBeTrue();
|
|
1183
|
+
});
|
|
1184
|
+
});
|
|
1185
|
+
|
|
1186
|
+
it("allows aborting a request with a Promise", async function () {
|
|
1187
|
+
var timeout = $q.defer();
|
|
1188
|
+
$http
|
|
1189
|
+
.get("/mock/never", {
|
|
1190
|
+
timeout: timeout.promise,
|
|
1191
|
+
})
|
|
1192
|
+
.then(
|
|
1193
|
+
function () {},
|
|
1194
|
+
function (err) {
|
|
1195
|
+
response = err;
|
|
1196
|
+
},
|
|
1197
|
+
);
|
|
1198
|
+
timeout.resolve();
|
|
1199
|
+
$rootScope.$apply();
|
|
1200
|
+
expect(response.xhrStatus).toBe("abort");
|
|
1201
|
+
});
|
|
1202
|
+
|
|
1203
|
+
it("allows aborting a request after a timeout", async function () {
|
|
1204
|
+
await $http
|
|
1205
|
+
.get("/mock/never", {
|
|
1206
|
+
timeout: 50,
|
|
1207
|
+
})
|
|
1208
|
+
.then(
|
|
1209
|
+
function () {},
|
|
1210
|
+
function (err) {
|
|
1211
|
+
response = err;
|
|
1212
|
+
},
|
|
1213
|
+
);
|
|
1214
|
+
$rootScope.$apply();
|
|
1215
|
+
expect(response.xhrStatus).toBe("timeout");
|
|
1216
|
+
});
|
|
1217
|
+
|
|
1218
|
+
describe("pending requests", function () {
|
|
1219
|
+
it("are in the collection while pending", function (done) {
|
|
1220
|
+
$http.get("/mock/hello").then(() => {
|
|
1221
|
+
expect($http.pendingRequests.length).toBe(0);
|
|
1222
|
+
done();
|
|
1223
|
+
});
|
|
1224
|
+
$rootScope.$apply();
|
|
1225
|
+
|
|
1226
|
+
expect($http.pendingRequests).toBeDefined();
|
|
1227
|
+
expect($http.pendingRequests.length).toBe(1);
|
|
1228
|
+
expect($http.pendingRequests[0].url).toBe("/mock/hello");
|
|
1229
|
+
});
|
|
1230
|
+
|
|
1231
|
+
it("are also cleared on failure", async function () {
|
|
1232
|
+
await $http.get("/mock/401").then(
|
|
1233
|
+
() => {},
|
|
1234
|
+
(err) => {},
|
|
1235
|
+
);
|
|
1236
|
+
|
|
1237
|
+
$rootScope.$apply();
|
|
1238
|
+
|
|
1239
|
+
expect($http.pendingRequests.length).toBe(0);
|
|
1240
|
+
});
|
|
1241
|
+
});
|
|
1242
|
+
|
|
1243
|
+
describe("useApplyAsync", function () {
|
|
1244
|
+
beforeEach(function () {
|
|
1245
|
+
var injector = createInjector([
|
|
1246
|
+
"ng",
|
|
1247
|
+
function ($httpProvider) {
|
|
1248
|
+
$httpProvider.useApplyAsync(true);
|
|
1249
|
+
},
|
|
1250
|
+
]);
|
|
1251
|
+
$http = injector.get("$http");
|
|
1252
|
+
$rootScope = injector.get("$rootScope");
|
|
1253
|
+
});
|
|
1254
|
+
|
|
1255
|
+
it("does not resolve promise immediately when enabled", async function () {
|
|
1256
|
+
var resolvedSpy = jasmine.createSpy();
|
|
1257
|
+
$http.get("/mock/hello").then(resolvedSpy);
|
|
1258
|
+
$rootScope.$apply();
|
|
1259
|
+
|
|
1260
|
+
expect(resolvedSpy).not.toHaveBeenCalled();
|
|
1261
|
+
});
|
|
1262
|
+
|
|
1263
|
+
it("resolves promise later when enabled", async function () {
|
|
1264
|
+
var resolvedSpy = jasmine.createSpy();
|
|
1265
|
+
await $http.get("/mock/hello").then(resolvedSpy);
|
|
1266
|
+
$rootScope.$apply();
|
|
1267
|
+
|
|
1268
|
+
expect(resolvedSpy).toHaveBeenCalled();
|
|
1269
|
+
});
|
|
1270
|
+
});
|
|
1271
|
+
});
|
|
1272
|
+
|
|
1273
|
+
// describe("$http", () => {
|
|
1274
|
+
// let callback;
|
|
1275
|
+
// let mockedCookies;
|
|
1276
|
+
// const customParamSerializer = function (params) {
|
|
1277
|
+
// return Object.keys(params).join("_");
|
|
1278
|
+
// };
|
|
1279
|
+
|
|
1280
|
+
// beforeEach(
|
|
1281
|
+
// module(() => {
|
|
1282
|
+
// callback = jasmine.createSpy("done");
|
|
1283
|
+
// mockedCookies = {};
|
|
1284
|
+
// }),
|
|
1285
|
+
// );
|
|
1286
|
+
|
|
1287
|
+
// beforeEach(
|
|
1288
|
+
// module({
|
|
1289
|
+
// $$cookieReader() {
|
|
1290
|
+
// return mockedCookies;
|
|
1291
|
+
// },
|
|
1292
|
+
// customParamSerializer,
|
|
1293
|
+
// }),
|
|
1294
|
+
// );
|
|
1295
|
+
|
|
1296
|
+
// afterEach(inject(($exceptionHandler, $httpBackend, $rootScope) => {
|
|
1297
|
+
// forEach($exceptionHandler.errors, (e) => {
|
|
1298
|
+
// dump("Unhandled exception: ", e);
|
|
1299
|
+
// });
|
|
1300
|
+
|
|
1301
|
+
// if ($exceptionHandler.errors.length) {
|
|
1302
|
+
// throw "Unhandled exceptions trapped in $exceptionHandler!";
|
|
1303
|
+
// }
|
|
1304
|
+
|
|
1305
|
+
// $httpBackend.verifyNoOutstandingExpectation();
|
|
1306
|
+
// }));
|
|
1307
|
+
|
|
1308
|
+
// describe("$httpProvider", () => {
|
|
1309
|
+
// describe("interceptors", () => {
|
|
1310
|
+
// it("should chain request, requestReject, response and responseReject interceptors", () => {
|
|
1311
|
+
// module(($httpProvider) => {
|
|
1312
|
+
// let savedConfig;
|
|
1313
|
+
// let savedResponse;
|
|
1314
|
+
// $httpProvider.interceptors.push(($q) => ({
|
|
1315
|
+
// request(config) {
|
|
1316
|
+
// config.url += "/1";
|
|
1317
|
+
// savedConfig = config;
|
|
1318
|
+
// return $q.reject("/2");
|
|
1319
|
+
// },
|
|
1320
|
+
// }));
|
|
1321
|
+
// $httpProvider.interceptors.push(($q) => ({
|
|
1322
|
+
// requestError(error) {
|
|
1323
|
+
// savedConfig.url += error;
|
|
1324
|
+
// return $q.resolve(savedConfig);
|
|
1325
|
+
// },
|
|
1326
|
+
// }));
|
|
1327
|
+
// $httpProvider.interceptors.push(() => ({
|
|
1328
|
+
// responseError(rejection) {
|
|
1329
|
+
// savedResponse.data += rejection;
|
|
1330
|
+
// return savedResponse;
|
|
1331
|
+
// },
|
|
1332
|
+
// }));
|
|
1333
|
+
// $httpProvider.interceptors.push(($q) => ({
|
|
1334
|
+
// response(response) {
|
|
1335
|
+
// response.data += ":1";
|
|
1336
|
+
// savedResponse = response;
|
|
1337
|
+
// return $q.reject(":2");
|
|
1338
|
+
// },
|
|
1339
|
+
// }));
|
|
1340
|
+
// });
|
|
1341
|
+
// inject(($http, $httpBackend, $rootScope) => {
|
|
1342
|
+
// let response;
|
|
1343
|
+
// $httpBackend.expect("GET", "/url/1/2").respond("response");
|
|
1344
|
+
// await $http({ method: "GET", url: "/url" }).then((r) => {
|
|
1345
|
+
// response = r;
|
|
1346
|
+
// });
|
|
1347
|
+
// $rootScope.$apply();
|
|
1348
|
+
// $httpBackend.flush();
|
|
1349
|
+
// expect(response.data).toEqual("response:1:2");
|
|
1350
|
+
// });
|
|
1351
|
+
// });
|
|
1352
|
+
|
|
1353
|
+
// it("should verify order of execution", () => {
|
|
1354
|
+
// module(($httpProvider) => {
|
|
1355
|
+
// $httpProvider.interceptors.push(($q) => ({
|
|
1356
|
+
// request(config) {
|
|
1357
|
+
// config.url += "/outer";
|
|
1358
|
+
// return config;
|
|
1359
|
+
// },
|
|
1360
|
+
// response(response) {
|
|
1361
|
+
// response.data = `{${response.data}} outer`;
|
|
1362
|
+
// return response;
|
|
1363
|
+
// },
|
|
1364
|
+
// }));
|
|
1365
|
+
// $httpProvider.interceptors.push(($q) => ({
|
|
1366
|
+
// request(config) {
|
|
1367
|
+
// config.url += "/inner";
|
|
1368
|
+
// return config;
|
|
1369
|
+
// },
|
|
1370
|
+
// response(response) {
|
|
1371
|
+
// response.data = `{${response.data}} inner`;
|
|
1372
|
+
// return response;
|
|
1373
|
+
// },
|
|
1374
|
+
// }));
|
|
1375
|
+
// });
|
|
1376
|
+
// inject(($http, $httpBackend) => {
|
|
1377
|
+
// let response;
|
|
1378
|
+
// $httpBackend.expect("GET", "/url/outer/inner").respond("response");
|
|
1379
|
+
// await $http({ method: "GET", url: "/url" }).then((r) => {
|
|
1380
|
+
// response = r;
|
|
1381
|
+
// });
|
|
1382
|
+
// $httpBackend.flush();
|
|
1383
|
+
// expect(response.data).toEqual("{{response} inner} outer");
|
|
1384
|
+
// });
|
|
1385
|
+
// });
|
|
1386
|
+
// });
|
|
1387
|
+
|
|
1388
|
+
// describe("request interceptors", () => {
|
|
1389
|
+
// it("should pass request config as a promise", () => {
|
|
1390
|
+
// let run = false;
|
|
1391
|
+
// module(($httpProvider) => {
|
|
1392
|
+
// $httpProvider.interceptors.push(() => ({
|
|
1393
|
+
// request(config) {
|
|
1394
|
+
// expect(config.url).toEqual("/url");
|
|
1395
|
+
// expect(config.data).toEqual({ one: "two" });
|
|
1396
|
+
// expect(config.headers.foo).toEqual("bar");
|
|
1397
|
+
// run = true;
|
|
1398
|
+
// return config;
|
|
1399
|
+
// },
|
|
1400
|
+
// }));
|
|
1401
|
+
// });
|
|
1402
|
+
// inject(($http, $httpBackend, $rootScope) => {
|
|
1403
|
+
// $httpBackend.expect("POST", "/url").respond("");
|
|
1404
|
+
// await $http({
|
|
1405
|
+
// method: "POST",
|
|
1406
|
+
// url: "/url",
|
|
1407
|
+
// data: { one: "two" },
|
|
1408
|
+
// headers: { foo: "bar" },
|
|
1409
|
+
// });
|
|
1410
|
+
// $rootScope.$apply();
|
|
1411
|
+
// expect(run).toEqual(true);
|
|
1412
|
+
// });
|
|
1413
|
+
// });
|
|
1414
|
+
|
|
1415
|
+
// it("should allow manipulation of request", () => {
|
|
1416
|
+
// module(($httpProvider) => {
|
|
1417
|
+
// $httpProvider.interceptors.push(() => ({
|
|
1418
|
+
// request(config) {
|
|
1419
|
+
// config.url = "/intercepted";
|
|
1420
|
+
// config.headers.foo = "intercepted";
|
|
1421
|
+
// return config;
|
|
1422
|
+
// },
|
|
1423
|
+
// }));
|
|
1424
|
+
// });
|
|
1425
|
+
// inject(($http, $httpBackend, $rootScope) => {
|
|
1426
|
+
// $httpBackend
|
|
1427
|
+
// .expect(
|
|
1428
|
+
// "GET",
|
|
1429
|
+
// "/intercepted",
|
|
1430
|
+
// null,
|
|
1431
|
+
// (headers) => headers.foo === "intercepted",
|
|
1432
|
+
// )
|
|
1433
|
+
// .respond("");
|
|
1434
|
+
// $http.get("/url");
|
|
1435
|
+
// $rootScope.$apply();
|
|
1436
|
+
// });
|
|
1437
|
+
// });
|
|
1438
|
+
|
|
1439
|
+
// it("should allow replacement of the headers object", () => {
|
|
1440
|
+
// module(($httpProvider) => {
|
|
1441
|
+
// $httpProvider.interceptors.push(() => ({
|
|
1442
|
+
// request(config) {
|
|
1443
|
+
// config.headers = { foo: "intercepted" };
|
|
1444
|
+
// return config;
|
|
1445
|
+
// },
|
|
1446
|
+
// }));
|
|
1447
|
+
// });
|
|
1448
|
+
// inject(($http, $httpBackend, $rootScope) => {
|
|
1449
|
+
// $httpBackend
|
|
1450
|
+
// .expect("GET", "/url", null, (headers) =>
|
|
1451
|
+
// angular.equals(headers, { foo: "intercepted" }),
|
|
1452
|
+
// )
|
|
1453
|
+
// .respond("");
|
|
1454
|
+
// $http.get("/url");
|
|
1455
|
+
// $rootScope.$apply();
|
|
1456
|
+
// });
|
|
1457
|
+
// });
|
|
1458
|
+
|
|
1459
|
+
// it("should reject the http promise if an interceptor fails", () => {
|
|
1460
|
+
// const reason = new Error("interceptor failed");
|
|
1461
|
+
// module(($httpProvider) => {
|
|
1462
|
+
// $httpProvider.interceptors.push(($q) => ({
|
|
1463
|
+
// request(promise) {
|
|
1464
|
+
// return $q.reject(reason);
|
|
1465
|
+
// },
|
|
1466
|
+
// }));
|
|
1467
|
+
// });
|
|
1468
|
+
// inject(($http, $httpBackend, $rootScope) => {
|
|
1469
|
+
// const success = jasmine.createSpy();
|
|
1470
|
+
// const error = jasmine.createSpy();
|
|
1471
|
+
// $http.get("/url").then(success, error);
|
|
1472
|
+
// $rootScope.$apply();
|
|
1473
|
+
// expect(success).not.toHaveBeenCalled();
|
|
1474
|
+
// expect(error).toHaveBeenCalledWith(reason);
|
|
1475
|
+
// });
|
|
1476
|
+
// });
|
|
1477
|
+
|
|
1478
|
+
// it("should not manipulate the passed-in config", () => {
|
|
1479
|
+
// module(($httpProvider) => {
|
|
1480
|
+
// $httpProvider.interceptors.push(() => ({
|
|
1481
|
+
// request(config) {
|
|
1482
|
+
// config.url = "/intercepted";
|
|
1483
|
+
// config.headers.foo = "intercepted";
|
|
1484
|
+
// return config;
|
|
1485
|
+
// },
|
|
1486
|
+
// }));
|
|
1487
|
+
// });
|
|
1488
|
+
// inject(($http, $httpBackend, $rootScope) => {
|
|
1489
|
+
// const config = { headers: { foo: "bar" } };
|
|
1490
|
+
// const configCopy = angular.copy(config);
|
|
1491
|
+
// $httpBackend.expect("GET", "/intercepted").respond("");
|
|
1492
|
+
// $http.get("/url", config);
|
|
1493
|
+
// $rootScope.$apply();
|
|
1494
|
+
// expect(config).toEqual(configCopy);
|
|
1495
|
+
// $httpBackend.expect("POST", "/intercepted").respond("");
|
|
1496
|
+
// $http.post("/url", { bar: "baz" }, config);
|
|
1497
|
+
// $rootScope.$apply();
|
|
1498
|
+
// expect(config).toEqual(configCopy);
|
|
1499
|
+
// });
|
|
1500
|
+
// });
|
|
1501
|
+
|
|
1502
|
+
// it("should support interceptors defined as services", () => {
|
|
1503
|
+
// module(($provide, $httpProvider) => {
|
|
1504
|
+
// $provide.factory("myInterceptor", () => ({
|
|
1505
|
+
// request(config) {
|
|
1506
|
+
// config.url = "/intercepted";
|
|
1507
|
+
// return config;
|
|
1508
|
+
// },
|
|
1509
|
+
// }));
|
|
1510
|
+
// $httpProvider.interceptors.push("myInterceptor");
|
|
1511
|
+
// });
|
|
1512
|
+
// inject(($http, $httpBackend, $rootScope) => {
|
|
1513
|
+
// $httpBackend.expect("POST", "/intercepted").respond("");
|
|
1514
|
+
// $http.post("/url");
|
|
1515
|
+
// $rootScope.$apply();
|
|
1516
|
+
// });
|
|
1517
|
+
// });
|
|
1518
|
+
|
|
1519
|
+
// it("should support complex interceptors based on promises", () => {
|
|
1520
|
+
// module(($provide, $httpProvider) => {
|
|
1521
|
+
// $provide.factory("myInterceptor", ($q, $rootScope) => ({
|
|
1522
|
+
// request(config) {
|
|
1523
|
+
// return $q.resolve("/intercepted").then((intercepted) => {
|
|
1524
|
+
// config.url = intercepted;
|
|
1525
|
+
// return config;
|
|
1526
|
+
// });
|
|
1527
|
+
// },
|
|
1528
|
+
// }));
|
|
1529
|
+
// $httpProvider.interceptors.push("myInterceptor");
|
|
1530
|
+
// });
|
|
1531
|
+
// inject(($http, $httpBackend, $rootScope) => {
|
|
1532
|
+
// $httpBackend.expect("POST", "/intercepted").respond("");
|
|
1533
|
+
// $http.post("/two");
|
|
1534
|
+
// $rootScope.$apply();
|
|
1535
|
+
// });
|
|
1536
|
+
// });
|
|
1537
|
+
// });
|
|
1538
|
+
// });
|
|
1539
|
+
|
|
1540
|
+
// describe("the instance", () => {
|
|
1541
|
+
// let $httpBackend;
|
|
1542
|
+
// let $http;
|
|
1543
|
+
// let $rootScope;
|
|
1544
|
+
// let $sce;
|
|
1545
|
+
|
|
1546
|
+
// beforeEach(
|
|
1547
|
+
// module(($sceDelegateProvider) => {
|
|
1548
|
+
// // Setup a special trusted url that we can use in testing JSONP requests
|
|
1549
|
+
// $sceDelegateProvider.trustedResourceUrlList([
|
|
1550
|
+
// "http://special.trusted.resource.com/**",
|
|
1551
|
+
// ]);
|
|
1552
|
+
// }),
|
|
1553
|
+
// );
|
|
1554
|
+
|
|
1555
|
+
// beforeEach(inject([
|
|
1556
|
+
// "$httpBackend",
|
|
1557
|
+
// "$http",
|
|
1558
|
+
// "$rootScope",
|
|
1559
|
+
// "$sce",
|
|
1560
|
+
// function ($hb, $h, $rs, $sc) {
|
|
1561
|
+
// $httpBackend = $hb;
|
|
1562
|
+
// $http = $h;
|
|
1563
|
+
// $rootScope = $rs;
|
|
1564
|
+
// $sce = $sc;
|
|
1565
|
+
// spyOn($rootScope, "$apply").and.callThrough();
|
|
1566
|
+
// },
|
|
1567
|
+
// ]));
|
|
1568
|
+
|
|
1569
|
+
// it("should throw error if the request configuration is not an object", () => {
|
|
1570
|
+
// expect(() => {
|
|
1571
|
+
// $http("/url");
|
|
1572
|
+
// }).toThrow(
|
|
1573
|
+
// "$http",
|
|
1574
|
+
// "badreq",
|
|
1575
|
+
// "Http request configuration must be an object. Received: /url",
|
|
1576
|
+
// );
|
|
1577
|
+
// });
|
|
1578
|
+
|
|
1579
|
+
// it("should throw error if the request configuration url is not a string nor a trusted object", () => {
|
|
1580
|
+
// expect(() => {
|
|
1581
|
+
// await $http({ url: false });
|
|
1582
|
+
// }).toThrow(
|
|
1583
|
+
// "$http",
|
|
1584
|
+
// "badreq",
|
|
1585
|
+
// "Http request configuration url must be a string or a $sce trusted object. Received: false",
|
|
1586
|
+
// );
|
|
1587
|
+
// expect(() => {
|
|
1588
|
+
// await $http({ url: null });
|
|
1589
|
+
// }).toThrow(
|
|
1590
|
+
// "$http",
|
|
1591
|
+
// "badreq",
|
|
1592
|
+
// "Http request configuration url must be a string or a $sce trusted object. Received: null",
|
|
1593
|
+
// );
|
|
1594
|
+
// expect(() => {
|
|
1595
|
+
// await $http({ url: 42 });
|
|
1596
|
+
// }).toThrow(
|
|
1597
|
+
// "$http",
|
|
1598
|
+
// "badreq",
|
|
1599
|
+
// "Http request configuration url must be a string or a $sce trusted object. Received: 42",
|
|
1600
|
+
// );
|
|
1601
|
+
// expect(() => {
|
|
1602
|
+
// await $http({});
|
|
1603
|
+
// }).toThrow(
|
|
1604
|
+
// "$http",
|
|
1605
|
+
// "badreq",
|
|
1606
|
+
// "Http request configuration url must be a string or a $sce trusted object. Received: undefined",
|
|
1607
|
+
// );
|
|
1608
|
+
// });
|
|
1609
|
+
|
|
1610
|
+
// it("should accept a $sce trusted object for the request configuration url", () => {
|
|
1611
|
+
// $httpBackend.expect("GET", "/url").respond("");
|
|
1612
|
+
// await $http({ url: $sce.trustAsResourceUrl("/url") });
|
|
1613
|
+
// });
|
|
1614
|
+
|
|
1615
|
+
// it("should send GET requests if no method specified", () => {
|
|
1616
|
+
// $httpBackend.expect("GET", "/url").respond("");
|
|
1617
|
+
// await $http({ url: "/url" });
|
|
1618
|
+
// });
|
|
1619
|
+
|
|
1620
|
+
// it("should do basic request", () => {
|
|
1621
|
+
// $httpBackend.expect("GET", "/url").respond("");
|
|
1622
|
+
// await $http({ url: "/url", method: "GET" });
|
|
1623
|
+
// });
|
|
1624
|
+
|
|
1625
|
+
// it("should pass data if specified", () => {
|
|
1626
|
+
// $httpBackend.expect("POST", "/url", "some-data").respond("");
|
|
1627
|
+
// await $http({ url: "/url", method: "POST", data: "some-data" });
|
|
1628
|
+
// });
|
|
1629
|
+
|
|
1630
|
+
// describe("params", () => {
|
|
1631
|
+
// it("should do basic request with params and encode", () => {
|
|
1632
|
+
// $httpBackend.expect("GET", "/url?a%3D=%3F%26&b=2").respond("");
|
|
1633
|
+
// await $http({ url: "/url", params: { "a=": "?&", b: 2 }, method: "GET" });
|
|
1634
|
+
// });
|
|
1635
|
+
|
|
1636
|
+
// it("should merge params if url contains some already", () => {
|
|
1637
|
+
// $httpBackend.expect("GET", "/url?c=3&a=1&b=2").respond("");
|
|
1638
|
+
// await $http({ url: "/url?c=3", params: { a: 1, b: 2 }, method: "GET" });
|
|
1639
|
+
// });
|
|
1640
|
+
|
|
1641
|
+
// it("should jsonify objects in params map", () => {
|
|
1642
|
+
// $httpBackend.expect("GET", "/url?a=1&b=%7B%22c%22:3%7D").respond("");
|
|
1643
|
+
// await $http({ url: "/url", params: { a: 1, b: { c: 3 } }, method: "GET" });
|
|
1644
|
+
// });
|
|
1645
|
+
|
|
1646
|
+
// it("should expand arrays in params map", () => {
|
|
1647
|
+
// $httpBackend.expect("GET", "/url?a=1&a=2&a=3").respond("");
|
|
1648
|
+
// await $http({ url: "/url", params: { a: [1, 2, 3] }, method: "GET" });
|
|
1649
|
+
// });
|
|
1650
|
+
|
|
1651
|
+
// it("should not encode @ in url params", () => {
|
|
1652
|
+
// // encodeURIComponent is too aggressive and doesn't follow http://www.ietf.org/rfc/rfc3986.txt
|
|
1653
|
+
// // with regards to the character set (pchar) allowed in path segments
|
|
1654
|
+
// // so we need this test to make sure that we don't over-encode the params and break stuff
|
|
1655
|
+
// // like buzz api which uses @self
|
|
1656
|
+
|
|
1657
|
+
// $httpBackend
|
|
1658
|
+
// .expect("GET", "/Path?!do%26h=g%3Da+h&:bar=$baz@1")
|
|
1659
|
+
// .respond("");
|
|
1660
|
+
// await $http({
|
|
1661
|
+
// url: "/Path",
|
|
1662
|
+
// params: { ":bar": "$baz@1", "!do&h": "g=a h" },
|
|
1663
|
+
// method: "GET",
|
|
1664
|
+
// });
|
|
1665
|
+
// });
|
|
1666
|
+
|
|
1667
|
+
// it("should not add question mark when params is empty", () => {
|
|
1668
|
+
// $httpBackend.expect("GET", "/url").respond("");
|
|
1669
|
+
// await $http({ url: "/url", params: {}, method: "GET" });
|
|
1670
|
+
// });
|
|
1671
|
+
|
|
1672
|
+
// it("should not double quote dates", () => {
|
|
1673
|
+
// $httpBackend
|
|
1674
|
+
// .expect("GET", "/url?date=2014-07-15T17:30:00.000Z")
|
|
1675
|
+
// .respond("");
|
|
1676
|
+
// await $http({
|
|
1677
|
+
// url: "/url",
|
|
1678
|
+
// params: { date: new Date("2014-07-15T17:30:00.000Z") },
|
|
1679
|
+
// method: "GET",
|
|
1680
|
+
// });
|
|
1681
|
+
// });
|
|
1682
|
+
|
|
1683
|
+
// describe("custom params serialization", () => {
|
|
1684
|
+
// it("should allow specifying custom paramSerializer as function", () => {
|
|
1685
|
+
// $httpBackend.expect("GET", "/url?foo_bar").respond("");
|
|
1686
|
+
// await $http({
|
|
1687
|
+
// url: "/url",
|
|
1688
|
+
// params: { foo: "fooVal", bar: "barVal" },
|
|
1689
|
+
// paramSerializer: customParamSerializer,
|
|
1690
|
+
// });
|
|
1691
|
+
// });
|
|
1692
|
+
|
|
1693
|
+
// it("should allow specifying custom paramSerializer as function from DI", () => {
|
|
1694
|
+
// $httpBackend.expect("GET", "/url?foo_bar").respond("");
|
|
1695
|
+
// await $http({
|
|
1696
|
+
// url: "/url",
|
|
1697
|
+
// params: { foo: "fooVal", bar: "barVal" },
|
|
1698
|
+
// paramSerializer: "customParamSerializer",
|
|
1699
|
+
// });
|
|
1700
|
+
// });
|
|
1701
|
+
// });
|
|
1702
|
+
// });
|
|
1703
|
+
|
|
1704
|
+
// describe("callbacks", () => {
|
|
1705
|
+
// it("should pass in the response object when a request is successful", () => {
|
|
1706
|
+
// $httpBackend
|
|
1707
|
+
// .expect("GET", "/url")
|
|
1708
|
+
// .respond(207, "my content", { "content-encoding": "smurf" });
|
|
1709
|
+
// await $http({ url: "/url", method: "GET" }).then((response) => {
|
|
1710
|
+
// expect(response.data).toBe("my content");
|
|
1711
|
+
// expect(response.status).toBe(207);
|
|
1712
|
+
// expect(response.headers()).toEqual(
|
|
1713
|
+
// extend(Object.create(null), { "content-encoding": "smurf" }),
|
|
1714
|
+
// );
|
|
1715
|
+
// expect(response.config.url).toBe("/url");
|
|
1716
|
+
// callback();
|
|
1717
|
+
// });
|
|
1718
|
+
|
|
1719
|
+
// $httpBackend.flush();
|
|
1720
|
+
// expect(callback).toHaveBeenCalled();
|
|
1721
|
+
// });
|
|
1722
|
+
|
|
1723
|
+
// it("should pass statusText in response object when a request is successful", () => {
|
|
1724
|
+
// $httpBackend.expect("GET", "/url").respond(200, "SUCCESS", {}, "OK");
|
|
1725
|
+
// await $http({ url: "/url", method: "GET" }).then((response) => {
|
|
1726
|
+
// expect(response.statusText).toBe("OK");
|
|
1727
|
+
// callback();
|
|
1728
|
+
// });
|
|
1729
|
+
|
|
1730
|
+
// $httpBackend.flush();
|
|
1731
|
+
// expect(callback).toHaveBeenCalled();
|
|
1732
|
+
// });
|
|
1733
|
+
|
|
1734
|
+
// it("should pass statusText in response object when a request fails", () => {
|
|
1735
|
+
// $httpBackend
|
|
1736
|
+
// .expect("GET", "/url")
|
|
1737
|
+
// .respond(404, "ERROR", {}, "Not Found");
|
|
1738
|
+
// await $http({ url: "/url", method: "GET" }).then(null, (response) => {
|
|
1739
|
+
// expect(response.statusText).toBe("Not Found");
|
|
1740
|
+
// callback();
|
|
1741
|
+
// });
|
|
1742
|
+
|
|
1743
|
+
// $httpBackend.flush();
|
|
1744
|
+
// expect(callback).toHaveBeenCalled();
|
|
1745
|
+
// });
|
|
1746
|
+
|
|
1747
|
+
// it("should pass xhrStatus in response object when a request is successful", () => {
|
|
1748
|
+
// $httpBackend.expect("GET", "/url").respond(200, "SUCCESS", {}, "OK");
|
|
1749
|
+
// await $http({ url: "/url", method: "GET" }).then((response) => {
|
|
1750
|
+
// expect(response.xhrStatus).toBe("complete");
|
|
1751
|
+
// callback();
|
|
1752
|
+
// });
|
|
1753
|
+
|
|
1754
|
+
// $httpBackend.flush();
|
|
1755
|
+
// expect(callback).toHaveBeenCalled();
|
|
1756
|
+
// });
|
|
1757
|
+
|
|
1758
|
+
// it("should pass xhrStatus in response object when a request fails", () => {
|
|
1759
|
+
// $httpBackend
|
|
1760
|
+
// .expect("GET", "/url")
|
|
1761
|
+
// .respond(404, "ERROR", {}, "Not Found");
|
|
1762
|
+
// await $http({ url: "/url", method: "GET" }).then(null, (response) => {
|
|
1763
|
+
// expect(response.xhrStatus).toBe("complete");
|
|
1764
|
+
// callback();
|
|
1765
|
+
// });
|
|
1766
|
+
|
|
1767
|
+
// $httpBackend.flush();
|
|
1768
|
+
// expect(callback).toHaveBeenCalled();
|
|
1769
|
+
// });
|
|
1770
|
+
|
|
1771
|
+
// it("should pass in the response object when a request failed", () => {
|
|
1772
|
+
// $httpBackend
|
|
1773
|
+
// .expect("GET", "/url")
|
|
1774
|
+
// .respond(543, "bad error", { "request-id": "123" });
|
|
1775
|
+
// await $http({ url: "/url", method: "GET" }).then(null, (response) => {
|
|
1776
|
+
// expect(response.data).toBe("bad error");
|
|
1777
|
+
// expect(response.status).toBe(543);
|
|
1778
|
+
// expect(response.headers()).toEqual(
|
|
1779
|
+
// extend(Object.create(null), { "request-id": "123" }),
|
|
1780
|
+
// );
|
|
1781
|
+
// expect(response.config.url).toBe("/url");
|
|
1782
|
+
// callback();
|
|
1783
|
+
// });
|
|
1784
|
+
|
|
1785
|
+
// $httpBackend.flush();
|
|
1786
|
+
// expect(callback).toHaveBeenCalled();
|
|
1787
|
+
// });
|
|
1788
|
+
// });
|
|
1789
|
+
|
|
1790
|
+
// describe("response headers", () => {
|
|
1791
|
+
// it("should return single header", () => {
|
|
1792
|
+
// $httpBackend.expect("GET", "/url").respond("", { date: "date-val" });
|
|
1793
|
+
// callback.and.callFake((r) => {
|
|
1794
|
+
// expect(r.headers("date")).toBe("date-val");
|
|
1795
|
+
// });
|
|
1796
|
+
|
|
1797
|
+
// await $http({ url: "/url", method: "GET" }).then(callback);
|
|
1798
|
+
// $httpBackend.flush();
|
|
1799
|
+
|
|
1800
|
+
// expect(callback).toHaveBeenCalled();
|
|
1801
|
+
// });
|
|
1802
|
+
|
|
1803
|
+
// it("should return null when single header does not exist", () => {
|
|
1804
|
+
// $httpBackend
|
|
1805
|
+
// .expect("GET", "/url")
|
|
1806
|
+
// .respond("", { "Some-Header": "Fake" });
|
|
1807
|
+
// callback.and.callFake((r) => {
|
|
1808
|
+
// r.headers(); // we need that to get headers parsed first
|
|
1809
|
+
// expect(r.headers("nothing")).toBe(null);
|
|
1810
|
+
// });
|
|
1811
|
+
|
|
1812
|
+
// await $http({ url: "/url", method: "GET" }).then(callback);
|
|
1813
|
+
// $httpBackend.flush();
|
|
1814
|
+
|
|
1815
|
+
// expect(callback).toHaveBeenCalled();
|
|
1816
|
+
// });
|
|
1817
|
+
|
|
1818
|
+
// it("should return all headers as object", () => {
|
|
1819
|
+
// $httpBackend.expect("GET", "/url").respond("", {
|
|
1820
|
+
// "content-encoding": "gzip",
|
|
1821
|
+
// server: "Apache",
|
|
1822
|
+
// });
|
|
1823
|
+
|
|
1824
|
+
// callback.and.callFake((r) => {
|
|
1825
|
+
// expect(r.headers()).toEqual(
|
|
1826
|
+
// extend(Object.create(null), {
|
|
1827
|
+
// "content-encoding": "gzip",
|
|
1828
|
+
// server: "Apache",
|
|
1829
|
+
// }),
|
|
1830
|
+
// );
|
|
1831
|
+
// });
|
|
1832
|
+
|
|
1833
|
+
// await $http({ url: "/url", method: "GET" }).then(callback);
|
|
1834
|
+
// $httpBackend.flush();
|
|
1835
|
+
|
|
1836
|
+
// expect(callback).toHaveBeenCalled();
|
|
1837
|
+
// });
|
|
1838
|
+
|
|
1839
|
+
// it("should return empty object for jsonp request", () => {
|
|
1840
|
+
// callback.and.callFake((r) => {
|
|
1841
|
+
// expect(r.headers()).toEqual(Object.create(null));
|
|
1842
|
+
// });
|
|
1843
|
+
|
|
1844
|
+
// $httpBackend
|
|
1845
|
+
// .expect("JSONP", "/some?callback=JSON_CALLBACK")
|
|
1846
|
+
// .respond(200);
|
|
1847
|
+
// await $http({ url: $sce.trustAsResourceUrl("/some"), method: "JSONP" }).then(
|
|
1848
|
+
// callback,
|
|
1849
|
+
// );
|
|
1850
|
+
// $httpBackend.flush();
|
|
1851
|
+
// expect(callback).toHaveBeenCalled();
|
|
1852
|
+
// });
|
|
1853
|
+
// });
|
|
1854
|
+
|
|
1855
|
+
// describe("response headers parser", () => {
|
|
1856
|
+
// /* global parseHeaders: false */
|
|
1857
|
+
|
|
1858
|
+
// it("should parse basic", () => {
|
|
1859
|
+
// const parsed = parseHeaders(
|
|
1860
|
+
// "date: Thu, 04 Aug 2011 20:23:08 GMT\n" +
|
|
1861
|
+
// "content-encoding: gzip\n" +
|
|
1862
|
+
// "transfer-encoding: chunked\n" +
|
|
1863
|
+
// "x-cache-info: not cacheable; response has already expired, not cacheable; response has already expired\n" +
|
|
1864
|
+
// "connection: Keep-Alive\n" +
|
|
1865
|
+
// "x-backend-server: pm-dekiwiki03\n" +
|
|
1866
|
+
// "pragma: no-cache\n" +
|
|
1867
|
+
// "server: Apache\n" +
|
|
1868
|
+
// "x-frame-options: DENY\n" +
|
|
1869
|
+
// "content-type: text/html; charset=utf-8\n" +
|
|
1870
|
+
// "vary: Cookie, Accept-Encoding\n" +
|
|
1871
|
+
// "keep-alive: timeout=5, max=1000\n" +
|
|
1872
|
+
// "expires: Thu: , 19 Nov 1981 08:52:00 GMT\n",
|
|
1873
|
+
// );
|
|
1874
|
+
|
|
1875
|
+
// expect(parsed.date).toBe("Thu, 04 Aug 2011 20:23:08 GMT");
|
|
1876
|
+
// expect(parsed["content-encoding"]).toBe("gzip");
|
|
1877
|
+
// expect(parsed["transfer-encoding"]).toBe("chunked");
|
|
1878
|
+
// expect(parsed["keep-alive"]).toBe("timeout=5, max=1000");
|
|
1879
|
+
// });
|
|
1880
|
+
|
|
1881
|
+
// it("should parse lines without space after colon", () => {
|
|
1882
|
+
// expect(parseHeaders("key:value").key).toBe("value");
|
|
1883
|
+
// });
|
|
1884
|
+
|
|
1885
|
+
// it("should trim the values", () => {
|
|
1886
|
+
// expect(parseHeaders("key: value ").key).toBe("value");
|
|
1887
|
+
// });
|
|
1888
|
+
|
|
1889
|
+
// it("should allow headers without value", () => {
|
|
1890
|
+
// expect(parseHeaders("key:").key).toBe("");
|
|
1891
|
+
// });
|
|
1892
|
+
|
|
1893
|
+
// it("should merge headers with same key", () => {
|
|
1894
|
+
// expect(parseHeaders("key: a\nkey:b\n").key).toBe("a, b");
|
|
1895
|
+
// });
|
|
1896
|
+
|
|
1897
|
+
// it("should normalize keys to lower case", () => {
|
|
1898
|
+
// expect(parseHeaders("KeY: value").key).toBe("value");
|
|
1899
|
+
// });
|
|
1900
|
+
|
|
1901
|
+
// it("should parse CRLF as delimiter", () => {
|
|
1902
|
+
// // IE does use CRLF
|
|
1903
|
+
// expect(parseHeaders("a: b\r\nc: d\r\n")).toEqual(
|
|
1904
|
+
// extend(Object.create(null), { a: "b", c: "d" }),
|
|
1905
|
+
// );
|
|
1906
|
+
// expect(parseHeaders("a: b\r\nc: d\r\n").a).toBe("b");
|
|
1907
|
+
// });
|
|
1908
|
+
|
|
1909
|
+
// it("should parse tab after semi-colon", () => {
|
|
1910
|
+
// expect(parseHeaders("a:\tbb").a).toBe("bb");
|
|
1911
|
+
// expect(parseHeaders("a: \tbb").a).toBe("bb");
|
|
1912
|
+
// });
|
|
1913
|
+
|
|
1914
|
+
// it("should parse multiple values for the same header", () => {
|
|
1915
|
+
// expect(parseHeaders("key:value1\nkey:value2").key).toBe(
|
|
1916
|
+
// "value1, value2",
|
|
1917
|
+
// );
|
|
1918
|
+
// });
|
|
1919
|
+
// });
|
|
1920
|
+
|
|
1921
|
+
// describe("request headers", () => {
|
|
1922
|
+
// it("should send custom headers", () => {
|
|
1923
|
+
// $httpBackend
|
|
1924
|
+
// .expect(
|
|
1925
|
+
// "GET",
|
|
1926
|
+
// "/url",
|
|
1927
|
+
// undefined,
|
|
1928
|
+
// (headers) => headers.Custom === "header",
|
|
1929
|
+
// )
|
|
1930
|
+
// .respond("");
|
|
1931
|
+
|
|
1932
|
+
// await $http({
|
|
1933
|
+
// url: "/url",
|
|
1934
|
+
// method: "GET",
|
|
1935
|
+
// headers: {
|
|
1936
|
+
// Custom: "header",
|
|
1937
|
+
// },
|
|
1938
|
+
// });
|
|
1939
|
+
|
|
1940
|
+
// $httpBackend.flush();
|
|
1941
|
+
// });
|
|
1942
|
+
|
|
1943
|
+
// it("should set default headers for GET request", () => {
|
|
1944
|
+
// $httpBackend
|
|
1945
|
+
// .expect(
|
|
1946
|
+
// "GET",
|
|
1947
|
+
// "/url",
|
|
1948
|
+
// undefined,
|
|
1949
|
+
// (headers) => headers.Accept === "application/json, text/plain, */*",
|
|
1950
|
+
// )
|
|
1951
|
+
// .respond("");
|
|
1952
|
+
|
|
1953
|
+
// await $http({ url: "/url", method: "GET", headers: {} });
|
|
1954
|
+
// $httpBackend.flush();
|
|
1955
|
+
// });
|
|
1956
|
+
|
|
1957
|
+
// it("should set default headers for POST request", () => {
|
|
1958
|
+
// $httpBackend
|
|
1959
|
+
// .expect(
|
|
1960
|
+
// "POST",
|
|
1961
|
+
// "/url",
|
|
1962
|
+
// "messageBody",
|
|
1963
|
+
// (headers) =>
|
|
1964
|
+
// headers.Accept === "application/json, text/plain, */*" &&
|
|
1965
|
+
// headers["Content-Type"] === "application/json;charset=utf-8",
|
|
1966
|
+
// )
|
|
1967
|
+
// .respond("");
|
|
1968
|
+
|
|
1969
|
+
// await $http({
|
|
1970
|
+
// url: "/url",
|
|
1971
|
+
// method: "POST",
|
|
1972
|
+
// headers: {},
|
|
1973
|
+
// data: "messageBody",
|
|
1974
|
+
// });
|
|
1975
|
+
// $httpBackend.flush();
|
|
1976
|
+
// });
|
|
1977
|
+
|
|
1978
|
+
// it("should set default headers for PUT request", () => {
|
|
1979
|
+
// $httpBackend
|
|
1980
|
+
// .expect(
|
|
1981
|
+
// "PUT",
|
|
1982
|
+
// "/url",
|
|
1983
|
+
// "messageBody",
|
|
1984
|
+
// (headers) =>
|
|
1985
|
+
// headers.Accept === "application/json, text/plain, */*" &&
|
|
1986
|
+
// headers["Content-Type"] === "application/json;charset=utf-8",
|
|
1987
|
+
// )
|
|
1988
|
+
// .respond("");
|
|
1989
|
+
|
|
1990
|
+
// await $http({ url: "/url", method: "PUT", headers: {}, data: "messageBody" });
|
|
1991
|
+
// $httpBackend.flush();
|
|
1992
|
+
// });
|
|
1993
|
+
|
|
1994
|
+
// it("should set default headers for PATCH request", () => {
|
|
1995
|
+
// $httpBackend
|
|
1996
|
+
// .expect(
|
|
1997
|
+
// "PATCH",
|
|
1998
|
+
// "/url",
|
|
1999
|
+
// "messageBody",
|
|
2000
|
+
// (headers) =>
|
|
2001
|
+
// headers.Accept === "application/json, text/plain, */*" &&
|
|
2002
|
+
// headers["Content-Type"] === "application/json;charset=utf-8",
|
|
2003
|
+
// )
|
|
2004
|
+
// .respond("");
|
|
2005
|
+
|
|
2006
|
+
// await $http({
|
|
2007
|
+
// url: "/url",
|
|
2008
|
+
// method: "PATCH",
|
|
2009
|
+
// headers: {},
|
|
2010
|
+
// data: "messageBody",
|
|
2011
|
+
// });
|
|
2012
|
+
// $httpBackend.flush();
|
|
2013
|
+
// });
|
|
2014
|
+
|
|
2015
|
+
// it("should set default headers for custom HTTP method", () => {
|
|
2016
|
+
// $httpBackend
|
|
2017
|
+
// .expect(
|
|
2018
|
+
// "FOO",
|
|
2019
|
+
// "/url",
|
|
2020
|
+
// undefined,
|
|
2021
|
+
// (headers) => headers.Accept === "application/json, text/plain, */*",
|
|
2022
|
+
// )
|
|
2023
|
+
// .respond("");
|
|
2024
|
+
|
|
2025
|
+
// await $http({ url: "/url", method: "FOO", headers: {} });
|
|
2026
|
+
// $httpBackend.flush();
|
|
2027
|
+
// });
|
|
2028
|
+
|
|
2029
|
+
// it("should override default headers with custom", () => {
|
|
2030
|
+
// $httpBackend
|
|
2031
|
+
// .expect(
|
|
2032
|
+
// "POST",
|
|
2033
|
+
// "/url",
|
|
2034
|
+
// "messageBody",
|
|
2035
|
+
// (headers) =>
|
|
2036
|
+
// headers.Accept === "Rewritten" &&
|
|
2037
|
+
// headers["Content-Type"] === "Rewritten",
|
|
2038
|
+
// )
|
|
2039
|
+
// .respond("");
|
|
2040
|
+
|
|
2041
|
+
// await $http({
|
|
2042
|
+
// url: "/url",
|
|
2043
|
+
// method: "POST",
|
|
2044
|
+
// data: "messageBody",
|
|
2045
|
+
// headers: {
|
|
2046
|
+
// Accept: "Rewritten",
|
|
2047
|
+
// "Content-Type": "Rewritten",
|
|
2048
|
+
// },
|
|
2049
|
+
// });
|
|
2050
|
+
// $httpBackend.flush();
|
|
2051
|
+
// });
|
|
2052
|
+
|
|
2053
|
+
// it("should delete default headers if custom header function returns null", () => {
|
|
2054
|
+
// $httpBackend
|
|
2055
|
+
// .expect(
|
|
2056
|
+
// "POST",
|
|
2057
|
+
// "/url",
|
|
2058
|
+
// "messageBody",
|
|
2059
|
+
// (headers) => !("Accept" in headers),
|
|
2060
|
+
// )
|
|
2061
|
+
// .respond("");
|
|
2062
|
+
|
|
2063
|
+
// await $http({
|
|
2064
|
+
// url: "/url",
|
|
2065
|
+
// method: "POST",
|
|
2066
|
+
// data: "messageBody",
|
|
2067
|
+
// headers: {
|
|
2068
|
+
// Accept: function() {
|
|
2069
|
+
// return null;
|
|
2070
|
+
// },
|
|
2071
|
+
// },
|
|
2072
|
+
// });
|
|
2073
|
+
// $httpBackend.flush();
|
|
2074
|
+
// });
|
|
2075
|
+
|
|
2076
|
+
// it("should override default headers with custom in a case insensitive manner", () => {
|
|
2077
|
+
// $httpBackend
|
|
2078
|
+
// .expect(
|
|
2079
|
+
// "POST",
|
|
2080
|
+
// "/url",
|
|
2081
|
+
// "messageBody",
|
|
2082
|
+
// (headers) =>
|
|
2083
|
+
// headers.accept === "Rewritten" &&
|
|
2084
|
+
// headers["content-type"] === "Content-Type Rewritten" &&
|
|
2085
|
+
// isUndefined(headers.Accept) &&
|
|
2086
|
+
// isUndefined(headers["Content-Type"]),
|
|
2087
|
+
// )
|
|
2088
|
+
// .respond("");
|
|
2089
|
+
|
|
2090
|
+
// await $http({
|
|
2091
|
+
// url: "/url",
|
|
2092
|
+
// method: "POST",
|
|
2093
|
+
// data: "messageBody",
|
|
2094
|
+
// headers: {
|
|
2095
|
+
// accept: "Rewritten",
|
|
2096
|
+
// "content-type": "Content-Type Rewritten",
|
|
2097
|
+
// },
|
|
2098
|
+
// });
|
|
2099
|
+
// $httpBackend.flush();
|
|
2100
|
+
// });
|
|
2101
|
+
|
|
2102
|
+
// it("should not send Content-Type header if request data/body is undefined", () => {
|
|
2103
|
+
// $httpBackend
|
|
2104
|
+
// .expect(
|
|
2105
|
+
// "POST",
|
|
2106
|
+
// "/url",
|
|
2107
|
+
// undefined,
|
|
2108
|
+
// (headers) => !headers.hasOwnProperty("Content-Type"),
|
|
2109
|
+
// )
|
|
2110
|
+
// .respond("");
|
|
2111
|
+
|
|
2112
|
+
// $httpBackend
|
|
2113
|
+
// .expect(
|
|
2114
|
+
// "POST",
|
|
2115
|
+
// "/url2",
|
|
2116
|
+
// undefined,
|
|
2117
|
+
// (headers) => !headers.hasOwnProperty("content-type"),
|
|
2118
|
+
// )
|
|
2119
|
+
// .respond("");
|
|
2120
|
+
|
|
2121
|
+
// await $http({ url: "/url", method: "POST" });
|
|
2122
|
+
// await $http({
|
|
2123
|
+
// url: "/url2",
|
|
2124
|
+
// method: "POST",
|
|
2125
|
+
// headers: { "content-type": "Rewritten" },
|
|
2126
|
+
// });
|
|
2127
|
+
// $httpBackend.flush();
|
|
2128
|
+
// });
|
|
2129
|
+
|
|
2130
|
+
// it("should NOT delete Content-Type header if request data/body is set by request transform", () => {
|
|
2131
|
+
// $httpBackend
|
|
2132
|
+
// .expect(
|
|
2133
|
+
// "POST",
|
|
2134
|
+
// "/url",
|
|
2135
|
+
// { one: "two" },
|
|
2136
|
+
// (headers) =>
|
|
2137
|
+
// headers["Content-Type"] === "application/json;charset=utf-8",
|
|
2138
|
+
// )
|
|
2139
|
+
// .respond("");
|
|
2140
|
+
|
|
2141
|
+
// await $http({
|
|
2142
|
+
// url: "/url",
|
|
2143
|
+
// method: "POST",
|
|
2144
|
+
// transformRequest(data) {
|
|
2145
|
+
// data = { one: "two" };
|
|
2146
|
+
// return data;
|
|
2147
|
+
// },
|
|
2148
|
+
// });
|
|
2149
|
+
|
|
2150
|
+
// $httpBackend.flush();
|
|
2151
|
+
// });
|
|
2152
|
+
|
|
2153
|
+
// it("should send execute result if header value is function", () => {
|
|
2154
|
+
// const headerConfig = {
|
|
2155
|
+
// Accept: function () {
|
|
2156
|
+
// return "Rewritten";
|
|
2157
|
+
// },
|
|
2158
|
+
// };
|
|
2159
|
+
|
|
2160
|
+
// function checkHeaders(headers) {
|
|
2161
|
+
// return headers.Accept === "Rewritten";
|
|
2162
|
+
// }
|
|
2163
|
+
|
|
2164
|
+
// $httpBackend.expect("GET", "/url", undefined, checkHeaders).respond("");
|
|
2165
|
+
// $httpBackend
|
|
2166
|
+
// .expect("POST", "/url", undefined, checkHeaders)
|
|
2167
|
+
// .respond("");
|
|
2168
|
+
// $httpBackend.expect("PUT", "/url", undefined, checkHeaders).respond("");
|
|
2169
|
+
// $httpBackend
|
|
2170
|
+
// .expect("PATCH", "/url", undefined, checkHeaders)
|
|
2171
|
+
// .respond("");
|
|
2172
|
+
// $httpBackend
|
|
2173
|
+
// .expect("DELETE", "/url", undefined, checkHeaders)
|
|
2174
|
+
// .respond("");
|
|
2175
|
+
|
|
2176
|
+
// await $http({ url: "/url", method: "GET", headers: headerConfig });
|
|
2177
|
+
// await $http({ url: "/url", method: "POST", headers: headerConfig });
|
|
2178
|
+
// await $http({ url: "/url", method: "PUT", headers: headerConfig });
|
|
2179
|
+
// await $http({ url: "/url", method: "PATCH", headers: headerConfig });
|
|
2180
|
+
// await $http({ url: "/url", method: "DELETE", headers: headerConfig });
|
|
2181
|
+
|
|
2182
|
+
// $httpBackend.flush();
|
|
2183
|
+
// });
|
|
2184
|
+
|
|
2185
|
+
// it("should expose a config object to header functions", () => {
|
|
2186
|
+
// const config = {
|
|
2187
|
+
// foo: "Rewritten",
|
|
2188
|
+
// headers: {
|
|
2189
|
+
// Accept: function (config) {
|
|
2190
|
+
// return config.foo;
|
|
2191
|
+
// },
|
|
2192
|
+
// },
|
|
2193
|
+
// };
|
|
2194
|
+
|
|
2195
|
+
// $httpBackend
|
|
2196
|
+
// .expect("GET", "/url", undefined, { Accept: "Rewritten" })
|
|
2197
|
+
// .respond("");
|
|
2198
|
+
// $http.get("/url", config);
|
|
2199
|
+
// $httpBackend.flush();
|
|
2200
|
+
// });
|
|
2201
|
+
|
|
2202
|
+
// it("should not allow modifications to a config object in header functions", () => {
|
|
2203
|
+
// const config = {
|
|
2204
|
+
// headers: {
|
|
2205
|
+
// Accept: function (config) {
|
|
2206
|
+
// config.foo = "bar";
|
|
2207
|
+
// return "Rewritten";
|
|
2208
|
+
// },
|
|
2209
|
+
// },
|
|
2210
|
+
// };
|
|
2211
|
+
|
|
2212
|
+
// $httpBackend
|
|
2213
|
+
// .expect("GET", "/url", undefined, { Accept: "Rewritten" })
|
|
2214
|
+
// .respond("");
|
|
2215
|
+
// $http.get("/url", config);
|
|
2216
|
+
// $httpBackend.flush();
|
|
2217
|
+
|
|
2218
|
+
// expect(config.foo).toBeUndefined();
|
|
2219
|
+
// });
|
|
2220
|
+
// });
|
|
2221
|
+
|
|
2222
|
+
// describe("short methods", () => {
|
|
2223
|
+
// function checkHeader(name, value) {
|
|
2224
|
+
// return function (headers) {
|
|
2225
|
+
// return headers[name] === value;
|
|
2226
|
+
// };
|
|
2227
|
+
// }
|
|
2228
|
+
|
|
2229
|
+
// it("should have get()", () => {
|
|
2230
|
+
// $httpBackend.expect("GET", "/url").respond("");
|
|
2231
|
+
// $http.get("/url");
|
|
2232
|
+
// });
|
|
2233
|
+
|
|
2234
|
+
// it("get() should allow config param", () => {
|
|
2235
|
+
// $httpBackend
|
|
2236
|
+
// .expect("GET", "/url", undefined, checkHeader("Custom", "Header"))
|
|
2237
|
+
// .respond("");
|
|
2238
|
+
// $http.get("/url", { headers: { Custom: "Header" } });
|
|
2239
|
+
// });
|
|
2240
|
+
|
|
2241
|
+
// it("should handle empty response header", () => {
|
|
2242
|
+
// $httpBackend.expect("GET", "/url", undefined).respond(200, "", {
|
|
2243
|
+
// "Custom-Empty-Response-Header": "",
|
|
2244
|
+
// Constructor: "",
|
|
2245
|
+
// });
|
|
2246
|
+
// $http.get("/url").then(callback);
|
|
2247
|
+
// $httpBackend.flush();
|
|
2248
|
+
// expect(callback).toHaveBeenCalled();
|
|
2249
|
+
// const { headers } = callback.calls.mostRecent().args[0];
|
|
2250
|
+
// expect(headers("custom-empty-response-Header")).toEqual("");
|
|
2251
|
+
// expect(headers("ToString")).toBe(null);
|
|
2252
|
+
// expect(headers("Constructor")).toBe("");
|
|
2253
|
+
// });
|
|
2254
|
+
|
|
2255
|
+
// it("should have delete()", () => {
|
|
2256
|
+
// $httpBackend.expect("DELETE", "/url").respond("");
|
|
2257
|
+
// $http.delete("/url");
|
|
2258
|
+
// });
|
|
2259
|
+
|
|
2260
|
+
// it("delete() should allow config param", () => {
|
|
2261
|
+
// $httpBackend
|
|
2262
|
+
// .expect("DELETE", "/url", undefined, checkHeader("Custom", "Header"))
|
|
2263
|
+
// .respond("");
|
|
2264
|
+
// $http.delete("/url", { headers: { Custom: "Header" } });
|
|
2265
|
+
// });
|
|
2266
|
+
|
|
2267
|
+
// it("should have head()", () => {
|
|
2268
|
+
// $httpBackend.expect("HEAD", "/url").respond("");
|
|
2269
|
+
// $http.head("/url");
|
|
2270
|
+
// });
|
|
2271
|
+
|
|
2272
|
+
// it("head() should allow config param", () => {
|
|
2273
|
+
// $httpBackend
|
|
2274
|
+
// .expect("HEAD", "/url", undefined, checkHeader("Custom", "Header"))
|
|
2275
|
+
// .respond("");
|
|
2276
|
+
// $http.head("/url", { headers: { Custom: "Header" } });
|
|
2277
|
+
// });
|
|
2278
|
+
|
|
2279
|
+
// it("should have post()", () => {
|
|
2280
|
+
// $httpBackend.expect("POST", "/url", "some-data").respond("");
|
|
2281
|
+
// $http.post("/url", "some-data");
|
|
2282
|
+
// });
|
|
2283
|
+
|
|
2284
|
+
// it("post() should allow config param", () => {
|
|
2285
|
+
// $httpBackend
|
|
2286
|
+
// .expect("POST", "/url", "some-data", checkHeader("Custom", "Header"))
|
|
2287
|
+
// .respond("");
|
|
2288
|
+
// $http.post("/url", "some-data", { headers: { Custom: "Header" } });
|
|
2289
|
+
// });
|
|
2290
|
+
|
|
2291
|
+
// it("should have put()", () => {
|
|
2292
|
+
// $httpBackend.expect("PUT", "/url", "some-data").respond("");
|
|
2293
|
+
// $http.put("/url", "some-data");
|
|
2294
|
+
// });
|
|
2295
|
+
|
|
2296
|
+
// it("put() should allow config param", () => {
|
|
2297
|
+
// $httpBackend
|
|
2298
|
+
// .expect("PUT", "/url", "some-data", checkHeader("Custom", "Header"))
|
|
2299
|
+
// .respond("");
|
|
2300
|
+
// $http.put("/url", "some-data", { headers: { Custom: "Header" } });
|
|
2301
|
+
// });
|
|
2302
|
+
|
|
2303
|
+
// it("should have patch()", () => {
|
|
2304
|
+
// $httpBackend.expect("PATCH", "/url", "some-data").respond("");
|
|
2305
|
+
// $http.patch("/url", "some-data");
|
|
2306
|
+
// });
|
|
2307
|
+
|
|
2308
|
+
// it("patch() should allow config param", () => {
|
|
2309
|
+
// $httpBackend
|
|
2310
|
+
// .expect("PATCH", "/url", "some-data", checkHeader("Custom", "Header"))
|
|
2311
|
+
// .respond("");
|
|
2312
|
+
// $http.patch("/url", "some-data", { headers: { Custom: "Header" } });
|
|
2313
|
+
// });
|
|
2314
|
+
|
|
2315
|
+
// });
|
|
2316
|
+
// describe("callbacks", () => {
|
|
2317
|
+
// it("should $apply after success callback", () => {
|
|
2318
|
+
// $httpBackend.when("GET").respond(200);
|
|
2319
|
+
// await $http({ method: "GET", url: "/some" });
|
|
2320
|
+
// $httpBackend.flush();
|
|
2321
|
+
// expect($rootScope.$apply).toHaveBeenCalled();
|
|
2322
|
+
// });
|
|
2323
|
+
|
|
2324
|
+
// it("should $apply after error callback", () => {
|
|
2325
|
+
// $httpBackend.when("GET").respond(404);
|
|
2326
|
+
// await $http({ method: "GET", url: "/some" }).catch(() => {});
|
|
2327
|
+
// $httpBackend.flush();
|
|
2328
|
+
// expect($rootScope.$apply).toHaveBeenCalled();
|
|
2329
|
+
// });
|
|
2330
|
+
|
|
2331
|
+
// it("should $apply even if exception thrown during callback", inject((
|
|
2332
|
+
// $exceptionHandler,
|
|
2333
|
+
// ) => {
|
|
2334
|
+
// $httpBackend.when("GET").respond(200);
|
|
2335
|
+
// callback.and.throwError("error in callback");
|
|
2336
|
+
|
|
2337
|
+
// await $http({ method: "GET", url: "/some" }).then(callback);
|
|
2338
|
+
// $httpBackend.flush();
|
|
2339
|
+
// expect($rootScope.$apply).toHaveBeenCalled();
|
|
2340
|
+
|
|
2341
|
+
// $exceptionHandler.errors = [];
|
|
2342
|
+
// }));
|
|
2343
|
+
|
|
2344
|
+
// it("should pass the event handlers through to the backend", () => {
|
|
2345
|
+
// const progressFn = jasmine.createSpy("progressFn");
|
|
2346
|
+
// const uploadProgressFn = jasmine.createSpy("uploadProgressFn");
|
|
2347
|
+
// $httpBackend.when("GET").respond(200);
|
|
2348
|
+
// await $http({
|
|
2349
|
+
// method: "GET",
|
|
2350
|
+
// url: "/some",
|
|
2351
|
+
// eventHandlers: { progress: progressFn },
|
|
2352
|
+
// uploadEventHandlers: { progress: uploadProgressFn },
|
|
2353
|
+
// });
|
|
2354
|
+
// $rootScope.$apply();
|
|
2355
|
+
// const mockXHR = MockXhr.$$lastInstance;
|
|
2356
|
+
// expect(mockXHR.$$events.progress).toEqual(jasmine.any(Function));
|
|
2357
|
+
// expect(mockXHR.upload.$$events.progress).toEqual(jasmine.any(Function));
|
|
2358
|
+
|
|
2359
|
+
// const eventObj = {};
|
|
2360
|
+
// spyOn($rootScope, "$digest");
|
|
2361
|
+
|
|
2362
|
+
// mockXHR.$$events.progress(eventObj);
|
|
2363
|
+
// expect(progressFn).toHaveBeenCalledOnceWith(eventObj);
|
|
2364
|
+
// expect($rootScope.$digest).toHaveBeenCalledTimes(1);
|
|
2365
|
+
|
|
2366
|
+
// mockXHR.upload.$$events.progress(eventObj);
|
|
2367
|
+
// expect(uploadProgressFn).toHaveBeenCalledOnceWith(eventObj);
|
|
2368
|
+
// expect($rootScope.$digest).toHaveBeenCalledTimes(2);
|
|
2369
|
+
// });
|
|
2370
|
+
// });
|
|
2371
|
+
|
|
2372
|
+
// describe("transformData", () => {
|
|
2373
|
+
// describe("request", () => {
|
|
2374
|
+
// describe("default", () => {
|
|
2375
|
+
// it("should transform object into json", () => {
|
|
2376
|
+
// $httpBackend.expect("POST", "/url", '{"one":"two"}').respond("");
|
|
2377
|
+
// await $http({ method: "POST", url: "/url", data: { one: "two" } });
|
|
2378
|
+
// });
|
|
2379
|
+
|
|
2380
|
+
// it("should transform object with date into json", () => {
|
|
2381
|
+
// $httpBackend
|
|
2382
|
+
// .expect("POST", "/url", {
|
|
2383
|
+
// date: new Date(Date.UTC(2013, 11, 25)),
|
|
2384
|
+
// })
|
|
2385
|
+
// .respond("");
|
|
2386
|
+
// await $http({
|
|
2387
|
+
// method: "POST",
|
|
2388
|
+
// url: "/url",
|
|
2389
|
+
// data: { date: new Date(Date.UTC(2013, 11, 25)) },
|
|
2390
|
+
// });
|
|
2391
|
+
// });
|
|
2392
|
+
|
|
2393
|
+
// it("should ignore strings", () => {
|
|
2394
|
+
// $httpBackend.expect("POST", "/url", "string-data").respond("");
|
|
2395
|
+
// await $http({ method: "POST", url: "/url", data: "string-data" });
|
|
2396
|
+
// });
|
|
2397
|
+
|
|
2398
|
+
// it("should ignore File objects", () => {
|
|
2399
|
+
// const file = {
|
|
2400
|
+
// some: true,
|
|
2401
|
+
// // $httpBackend compares toJson values by default,
|
|
2402
|
+
// // we need to be sure it's not serialized into json string
|
|
2403
|
+
// test(actualValue) {
|
|
2404
|
+
// return this === actualValue;
|
|
2405
|
+
// },
|
|
2406
|
+
// };
|
|
2407
|
+
|
|
2408
|
+
// // I'm really sorry for doing this :-D
|
|
2409
|
+
// // Unfortunately I don't know how to trick toString.apply(obj) comparison
|
|
2410
|
+
// spyOn(window, "isFile").and.returnValue(true);
|
|
2411
|
+
|
|
2412
|
+
// $httpBackend.expect("POST", "/some", file).respond("");
|
|
2413
|
+
// await $http({ method: "POST", url: "/some", data: file });
|
|
2414
|
+
// });
|
|
2415
|
+
// });
|
|
2416
|
+
|
|
2417
|
+
// it("should ignore Blob objects", () => {
|
|
2418
|
+
// if (!window.Blob) return;
|
|
2419
|
+
|
|
2420
|
+
// // eslint-disable-next-line no-undef
|
|
2421
|
+
// const blob = new Blob(["blob!"], { type: "text/plain" });
|
|
2422
|
+
|
|
2423
|
+
// $httpBackend.expect("POST", "/url", "[object Blob]").respond("");
|
|
2424
|
+
// await $http({ method: "POST", url: "/url", data: blob });
|
|
2425
|
+
// });
|
|
2426
|
+
|
|
2427
|
+
// it("should ignore FormData objects", () => {
|
|
2428
|
+
// if (!window.FormData) return;
|
|
2429
|
+
|
|
2430
|
+
// // eslint-disable-next-line no-undef
|
|
2431
|
+
// const formData = new FormData();
|
|
2432
|
+
// formData.append("angular", "is great");
|
|
2433
|
+
|
|
2434
|
+
// $httpBackend.expect("POST", "/url", "[object FormData]").respond("");
|
|
2435
|
+
// await $http({ method: "POST", url: "/url", data: formData });
|
|
2436
|
+
// });
|
|
2437
|
+
|
|
2438
|
+
// it("should have access to request headers", () => {
|
|
2439
|
+
// $httpBackend.expect("POST", "/url", "header1").respond(200);
|
|
2440
|
+
// $http
|
|
2441
|
+
// .post("/url", "req", {
|
|
2442
|
+
// headers: { h1: "header1" },
|
|
2443
|
+
// transformRequest(data, headers) {
|
|
2444
|
+
// return headers("h1");
|
|
2445
|
+
// },
|
|
2446
|
+
// })
|
|
2447
|
+
// .then(callback);
|
|
2448
|
+
// $httpBackend.flush();
|
|
2449
|
+
|
|
2450
|
+
// expect(callback).toHaveBeenCalled();
|
|
2451
|
+
// });
|
|
2452
|
+
|
|
2453
|
+
// it("should have access to request headers with mixed case", () => {
|
|
2454
|
+
// $httpBackend.expect("POST", "/url", "header1").respond(200);
|
|
2455
|
+
// $http
|
|
2456
|
+
// .post("/url", "req", {
|
|
2457
|
+
// headers: { H1: "header1" },
|
|
2458
|
+
// transformRequest(data, headers) {
|
|
2459
|
+
// return headers("H1");
|
|
2460
|
+
// },
|
|
2461
|
+
// })
|
|
2462
|
+
// .then(callback);
|
|
2463
|
+
// $httpBackend.flush();
|
|
2464
|
+
|
|
2465
|
+
// expect(callback).toHaveBeenCalled();
|
|
2466
|
+
// });
|
|
2467
|
+
|
|
2468
|
+
// it("should not allow modifications to headers in a transform functions", () => {
|
|
2469
|
+
// const config = {
|
|
2470
|
+
// headers: { Accept: "bar" },
|
|
2471
|
+
// transformRequest(data, headers) {
|
|
2472
|
+
// angular.extend(headers(), {
|
|
2473
|
+
// Accept: "foo",
|
|
2474
|
+
// });
|
|
2475
|
+
// },
|
|
2476
|
+
// };
|
|
2477
|
+
|
|
2478
|
+
// $httpBackend
|
|
2479
|
+
// .expect("GET", "/url", undefined, { Accept: "bar" })
|
|
2480
|
+
// .respond(200);
|
|
2481
|
+
// $http.get("/url", config).then(callback);
|
|
2482
|
+
// $httpBackend.flush();
|
|
2483
|
+
|
|
2484
|
+
// expect(callback).toHaveBeenCalled();
|
|
2485
|
+
// });
|
|
2486
|
+
|
|
2487
|
+
// it("should pipeline more functions", () => {
|
|
2488
|
+
// function first(d, h) {
|
|
2489
|
+
// return `${d}-first:${h("h1")}`;
|
|
2490
|
+
// }
|
|
2491
|
+
// function second(d) {
|
|
2492
|
+
// return uppercase(d);
|
|
2493
|
+
// }
|
|
2494
|
+
|
|
2495
|
+
// $httpBackend.expect("POST", "/url", "REQ-FIRST:V1").respond(200);
|
|
2496
|
+
// $http
|
|
2497
|
+
// .post("/url", "req", {
|
|
2498
|
+
// headers: { h1: "v1" },
|
|
2499
|
+
// transformRequest: [first, second],
|
|
2500
|
+
// })
|
|
2501
|
+
// .then(callback);
|
|
2502
|
+
// $httpBackend.flush();
|
|
2503
|
+
|
|
2504
|
+
// expect(callback).toHaveBeenCalled();
|
|
2505
|
+
// });
|
|
2506
|
+
// });
|
|
2507
|
+
|
|
2508
|
+
// describe("response", () => {
|
|
2509
|
+
// describe("default", () => {
|
|
2510
|
+
// it("should deserialize json objects", () => {
|
|
2511
|
+
// $httpBackend
|
|
2512
|
+
// .expect("GET", "/url")
|
|
2513
|
+
// .respond('{"foo":"bar","baz":23}');
|
|
2514
|
+
// await $http({ method: "GET", url: "/url" }).then(callback);
|
|
2515
|
+
// $httpBackend.flush();
|
|
2516
|
+
|
|
2517
|
+
// expect(callback).toHaveBeenCalled();
|
|
2518
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual({
|
|
2519
|
+
// foo: "bar",
|
|
2520
|
+
// baz: 23,
|
|
2521
|
+
// });
|
|
2522
|
+
// });
|
|
2523
|
+
|
|
2524
|
+
// it("should deserialize json arrays", () => {
|
|
2525
|
+
// $httpBackend
|
|
2526
|
+
// .expect("GET", "/url")
|
|
2527
|
+
// .respond('[1, "abc", {"foo":"bar"}]');
|
|
2528
|
+
// await $http({ method: "GET", url: "/url" }).then(callback);
|
|
2529
|
+
// $httpBackend.flush();
|
|
2530
|
+
|
|
2531
|
+
// expect(callback).toHaveBeenCalled();
|
|
2532
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual([
|
|
2533
|
+
// 1,
|
|
2534
|
+
// "abc",
|
|
2535
|
+
// { foo: "bar" },
|
|
2536
|
+
// ]);
|
|
2537
|
+
// });
|
|
2538
|
+
|
|
2539
|
+
// it("should ignore leading/trailing whitespace", () => {
|
|
2540
|
+
// $httpBackend
|
|
2541
|
+
// .expect("GET", "/url")
|
|
2542
|
+
// .respond(' \n {"foo":"bar","baz":23} \r\n \n ');
|
|
2543
|
+
// await $http({ method: "GET", url: "/url" }).then(callback);
|
|
2544
|
+
// $httpBackend.flush();
|
|
2545
|
+
|
|
2546
|
+
// expect(callback).toHaveBeenCalled();
|
|
2547
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual({
|
|
2548
|
+
// foo: "bar",
|
|
2549
|
+
// baz: 23,
|
|
2550
|
+
// });
|
|
2551
|
+
// });
|
|
2552
|
+
|
|
2553
|
+
// it("should deserialize json numbers when response header contains application/json", () => {
|
|
2554
|
+
// $httpBackend
|
|
2555
|
+
// .expect("GET", "/url")
|
|
2556
|
+
// .respond("123", { "Content-Type": "application/json" });
|
|
2557
|
+
// await $http({ method: "GET", url: "/url" }).then(callback);
|
|
2558
|
+
// $httpBackend.flush();
|
|
2559
|
+
|
|
2560
|
+
// expect(callback).toHaveBeenCalled();
|
|
2561
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual(123);
|
|
2562
|
+
// });
|
|
2563
|
+
|
|
2564
|
+
// it("should deserialize json strings when response header contains application/json", () => {
|
|
2565
|
+
// $httpBackend
|
|
2566
|
+
// .expect("GET", "/url")
|
|
2567
|
+
// .respond('"asdf"', { "Content-Type": "application/json" });
|
|
2568
|
+
// await $http({ method: "GET", url: "/url" }).then(callback);
|
|
2569
|
+
// $httpBackend.flush();
|
|
2570
|
+
|
|
2571
|
+
// expect(callback).toHaveBeenCalled();
|
|
2572
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual("asdf");
|
|
2573
|
+
// });
|
|
2574
|
+
|
|
2575
|
+
// it("should deserialize json nulls when response header contains application/json", () => {
|
|
2576
|
+
// $httpBackend
|
|
2577
|
+
// .expect("GET", "/url")
|
|
2578
|
+
// .respond("null", { "Content-Type": "application/json" });
|
|
2579
|
+
// await $http({ method: "GET", url: "/url" }).then(callback);
|
|
2580
|
+
// $httpBackend.flush();
|
|
2581
|
+
|
|
2582
|
+
// expect(callback).toHaveBeenCalled();
|
|
2583
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual(null);
|
|
2584
|
+
// });
|
|
2585
|
+
|
|
2586
|
+
// it("should deserialize json true when response header contains application/json", () => {
|
|
2587
|
+
// $httpBackend
|
|
2588
|
+
// .expect("GET", "/url")
|
|
2589
|
+
// .respond("true", { "Content-Type": "application/json" });
|
|
2590
|
+
// await $http({ method: "GET", url: "/url" }).then(callback);
|
|
2591
|
+
// $httpBackend.flush();
|
|
2592
|
+
|
|
2593
|
+
// expect(callback).toHaveBeenCalled();
|
|
2594
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual(true);
|
|
2595
|
+
// });
|
|
2596
|
+
|
|
2597
|
+
// it("should deserialize json false when response header contains application/json", () => {
|
|
2598
|
+
// $httpBackend
|
|
2599
|
+
// .expect("GET", "/url")
|
|
2600
|
+
// .respond("false", { "Content-Type": "application/json" });
|
|
2601
|
+
// await $http({ method: "GET", url: "/url" }).then(callback);
|
|
2602
|
+
// $httpBackend.flush();
|
|
2603
|
+
|
|
2604
|
+
// expect(callback).toHaveBeenCalled();
|
|
2605
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual(false);
|
|
2606
|
+
// });
|
|
2607
|
+
|
|
2608
|
+
// it("should deserialize json empty string when response header contains application/json", () => {
|
|
2609
|
+
// $httpBackend
|
|
2610
|
+
// .expect("GET", "/url")
|
|
2611
|
+
// .respond('""', { "Content-Type": "application/json" });
|
|
2612
|
+
// await $http({ method: "GET", url: "/url" }).then(callback);
|
|
2613
|
+
// $httpBackend.flush();
|
|
2614
|
+
|
|
2615
|
+
// expect(callback).toHaveBeenCalled();
|
|
2616
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual("");
|
|
2617
|
+
// });
|
|
2618
|
+
|
|
2619
|
+
// it("should deserialize json with security prefix", () => {
|
|
2620
|
+
// $httpBackend
|
|
2621
|
+
// .expect("GET", "/url")
|
|
2622
|
+
// .respond(')]}\',\n[1, "abc", {"foo":"bar"}]');
|
|
2623
|
+
// await $http({ method: "GET", url: "/url" }).then(callback);
|
|
2624
|
+
// $httpBackend.flush();
|
|
2625
|
+
|
|
2626
|
+
// expect(callback).toHaveBeenCalled();
|
|
2627
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual([
|
|
2628
|
+
// 1,
|
|
2629
|
+
// "abc",
|
|
2630
|
+
// { foo: "bar" },
|
|
2631
|
+
// ]);
|
|
2632
|
+
// });
|
|
2633
|
+
|
|
2634
|
+
// it('should deserialize json with security prefix ")]}\'"', () => {
|
|
2635
|
+
// $httpBackend
|
|
2636
|
+
// .expect("GET", "/url")
|
|
2637
|
+
// .respond(')]}\'\n\n[1, "abc", {"foo":"bar"}]');
|
|
2638
|
+
// await $http({ method: "GET", url: "/url" }).then(callback);
|
|
2639
|
+
// $httpBackend.flush();
|
|
2640
|
+
|
|
2641
|
+
// expect(callback).toHaveBeenCalled();
|
|
2642
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual([
|
|
2643
|
+
// 1,
|
|
2644
|
+
// "abc",
|
|
2645
|
+
// { foo: "bar" },
|
|
2646
|
+
// ]);
|
|
2647
|
+
// });
|
|
2648
|
+
|
|
2649
|
+
// it("should retain security prefix if response is not json", () => {
|
|
2650
|
+
// $httpBackend
|
|
2651
|
+
// .expect("GET", "/url")
|
|
2652
|
+
// .respond(")]}',\n This is not JSON !");
|
|
2653
|
+
// await $http({ method: "GET", url: "/url" }).then(callback);
|
|
2654
|
+
// $httpBackend.flush();
|
|
2655
|
+
|
|
2656
|
+
// expect(callback).toHaveBeenCalled();
|
|
2657
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual(
|
|
2658
|
+
// ")]}',\n This is not JSON !",
|
|
2659
|
+
// );
|
|
2660
|
+
// });
|
|
2661
|
+
|
|
2662
|
+
// it("should not attempt to deserialize json when HEAD request", () => {
|
|
2663
|
+
// // per http spec for Content-Type, HEAD request should return a Content-Type header
|
|
2664
|
+
// // set to what the content type would have been if a get was sent
|
|
2665
|
+
// $httpBackend
|
|
2666
|
+
// .expect("HEAD", "/url")
|
|
2667
|
+
// .respond("", { "Content-Type": "application/json" });
|
|
2668
|
+
// await $http({ method: "HEAD", url: "/url" }).then(callback);
|
|
2669
|
+
// $httpBackend.flush();
|
|
2670
|
+
|
|
2671
|
+
// expect(callback).toHaveBeenCalled();
|
|
2672
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual("");
|
|
2673
|
+
// });
|
|
2674
|
+
|
|
2675
|
+
// it("should not attempt to deserialize json for an empty response whose header contains application/json", () => {
|
|
2676
|
+
// // per http spec for Content-Type, HEAD request should return a Content-Type header
|
|
2677
|
+
// // set to what the content type would have been if a get was sent
|
|
2678
|
+
// $httpBackend
|
|
2679
|
+
// .expect("GET", "/url")
|
|
2680
|
+
// .respond("", { "Content-Type": "application/json" });
|
|
2681
|
+
// await $http({ method: "GET", url: "/url" }).then(callback);
|
|
2682
|
+
// $httpBackend.flush();
|
|
2683
|
+
|
|
2684
|
+
// expect(callback).toHaveBeenCalled();
|
|
2685
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual("");
|
|
2686
|
+
// });
|
|
2687
|
+
|
|
2688
|
+
// it("should not attempt to deserialize json for a blank response whose header contains application/json", () => {
|
|
2689
|
+
// // per http spec for Content-Type, HEAD request should return a Content-Type header
|
|
2690
|
+
// // set to what the content type would have been if a get was sent
|
|
2691
|
+
// $httpBackend
|
|
2692
|
+
// .expect("GET", "/url")
|
|
2693
|
+
// .respond(" ", { "Content-Type": "application/json" });
|
|
2694
|
+
// await $http({ method: "GET", url: "/url" }).then(callback);
|
|
2695
|
+
// $httpBackend.flush();
|
|
2696
|
+
|
|
2697
|
+
// expect(callback).toHaveBeenCalled();
|
|
2698
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual(" ");
|
|
2699
|
+
// });
|
|
2700
|
+
|
|
2701
|
+
// it("should not deserialize tpl beginning with ng expression", () => {
|
|
2702
|
+
// $httpBackend.expect("GET", "/url").respond("{{some}}");
|
|
2703
|
+
// $http.get("/url").then(callback);
|
|
2704
|
+
// $httpBackend.flush();
|
|
2705
|
+
|
|
2706
|
+
// expect(callback).toHaveBeenCalled();
|
|
2707
|
+
// expect(callback.calls.mostRecent().args[0].data).toEqual(
|
|
2708
|
+
// "{{some}}",
|
|
2709
|
+
// );
|
|
2710
|
+
// });
|
|
2711
|
+
|
|
2712
|
+
// it("should not deserialize json when the opening and closing brackets do not match", () => {
|
|
2713
|
+
// $httpBackend
|
|
2714
|
+
// .expect("GET", "/url1")
|
|
2715
|
+
// .respond("[Code](url): () => {}");
|
|
2716
|
+
// $httpBackend
|
|
2717
|
+
// .expect("GET", "/url2")
|
|
2718
|
+
// .respond('{"is": "not"} ["json"]');
|
|
2719
|
+
// $http.get("/url1").then(callback);
|
|
2720
|
+
// $http.get("/url2").then(callback);
|
|
2721
|
+
// $httpBackend.flush();
|
|
2722
|
+
|
|
2723
|
+
// expect(callback).toHaveBeenCalledTimes(2);
|
|
2724
|
+
// expect(callback.calls.argsFor(0)[0].data).toEqual(
|
|
2725
|
+
// "[Code](url): () => {}",
|
|
2726
|
+
// );
|
|
2727
|
+
// expect(callback.calls.argsFor(1)[0].data).toEqual(
|
|
2728
|
+
// '{"is": "not"} ["json"]',
|
|
2729
|
+
// );
|
|
2730
|
+
// });
|
|
2731
|
+
|
|
2732
|
+
// it("should return JSON data with error message if JSON is invalid", () => {
|
|
2733
|
+
// const errCallback = jasmine.createSpy("error");
|
|
2734
|
+
// $httpBackend
|
|
2735
|
+
// .expect("GET", "/url")
|
|
2736
|
+
// .respond("{abcd}", { "Content-Type": "application/json" });
|
|
2737
|
+
// $http.get("/url").then(callback).catch(errCallback);
|
|
2738
|
+
// $httpBackend.flush();
|
|
2739
|
+
|
|
2740
|
+
// expect(callback).not.toHaveBeenCalled();
|
|
2741
|
+
// expect(errCallback).toHaveBeenCalled();
|
|
2742
|
+
// expect(errCallback.calls.mostRecent().args[0]).toEqualMinErr(
|
|
2743
|
+
// "$http",
|
|
2744
|
+
// "baddata",
|
|
2745
|
+
// );
|
|
2746
|
+
// });
|
|
2747
|
+
|
|
2748
|
+
// it("should not throw an error if JSON is invalid but content-type is not application/json", () => {
|
|
2749
|
+
// $httpBackend
|
|
2750
|
+
// .expect("GET", "/url")
|
|
2751
|
+
// .respond("{abcd}", { "Content-Type": "text/plain" });
|
|
2752
|
+
|
|
2753
|
+
// $http.get("/url").then(callback);
|
|
2754
|
+
// $httpBackend.flush();
|
|
2755
|
+
|
|
2756
|
+
// expect(callback).toHaveBeenCalled();
|
|
2757
|
+
// });
|
|
2758
|
+
|
|
2759
|
+
// it("should not throw an error if JSON is invalid but content-type is not specified", () => {
|
|
2760
|
+
// $httpBackend.expect("GET", "/url").respond("{abcd}");
|
|
2761
|
+
|
|
2762
|
+
// $http.get("/url").then(callback);
|
|
2763
|
+
// $httpBackend.flush();
|
|
2764
|
+
|
|
2765
|
+
// expect(callback).toHaveBeenCalled();
|
|
2766
|
+
// });
|
|
2767
|
+
|
|
2768
|
+
// it("should return response unprocessed if JSON is invalid but content-type is not application/json", () => {
|
|
2769
|
+
// const response = "{abcd}";
|
|
2770
|
+
// $httpBackend
|
|
2771
|
+
// .expect("GET", "/url")
|
|
2772
|
+
// .respond(response, { "Content-Type": "text/plain" });
|
|
2773
|
+
|
|
2774
|
+
// $http.get("/url").then(callback);
|
|
2775
|
+
// $httpBackend.flush();
|
|
2776
|
+
|
|
2777
|
+
// expect(callback.calls.mostRecent().args[0].data).toBe(response);
|
|
2778
|
+
// });
|
|
2779
|
+
|
|
2780
|
+
// it("should return response unprocessed if JSON is invalid but content-type is not specified", () => {
|
|
2781
|
+
// const response = "{abcd}";
|
|
2782
|
+
// $httpBackend.expect("GET", "/url").respond(response);
|
|
2783
|
+
|
|
2784
|
+
// $http.get("/url").then(callback);
|
|
2785
|
+
// $httpBackend.flush();
|
|
2786
|
+
|
|
2787
|
+
// expect(callback.calls.mostRecent().args[0].data).toBe(response);
|
|
2788
|
+
// });
|
|
2789
|
+
// });
|
|
2790
|
+
|
|
2791
|
+
// it("should have access to response headers", () => {
|
|
2792
|
+
// $httpBackend
|
|
2793
|
+
// .expect("GET", "/url")
|
|
2794
|
+
// .respond(200, "response", { h1: "header1" });
|
|
2795
|
+
// $http
|
|
2796
|
+
// .get("/url", {
|
|
2797
|
+
// transformResponse(data, headers) {
|
|
2798
|
+
// return headers("h1");
|
|
2799
|
+
// },
|
|
2800
|
+
// })
|
|
2801
|
+
// .then(callback);
|
|
2802
|
+
// $httpBackend.flush();
|
|
2803
|
+
|
|
2804
|
+
// expect(callback).toHaveBeenCalled();
|
|
2805
|
+
// expect(callback.calls.mostRecent().args[0].data).toBe("header1");
|
|
2806
|
+
// });
|
|
2807
|
+
|
|
2808
|
+
// it("should have access to response status", () => {
|
|
2809
|
+
// $httpBackend
|
|
2810
|
+
// .expect("GET", "/url")
|
|
2811
|
+
// .respond(200, "response", { h1: "header1" });
|
|
2812
|
+
// $http
|
|
2813
|
+
// .get("/url", {
|
|
2814
|
+
// transformResponse(data, headers, status) {
|
|
2815
|
+
// return status;
|
|
2816
|
+
// },
|
|
2817
|
+
// })
|
|
2818
|
+
// .then(callback);
|
|
2819
|
+
// $httpBackend.flush();
|
|
2820
|
+
|
|
2821
|
+
// expect(callback).toHaveBeenCalled();
|
|
2822
|
+
// expect(callback.calls.mostRecent().args[0].data).toBe(200);
|
|
2823
|
+
// });
|
|
2824
|
+
|
|
2825
|
+
// it("should pipeline more functions", () => {
|
|
2826
|
+
// function first(d, h) {
|
|
2827
|
+
// return `${d}-first:${h("h1")}`;
|
|
2828
|
+
// }
|
|
2829
|
+
// function second(d) {
|
|
2830
|
+
// return uppercase(d);
|
|
2831
|
+
// }
|
|
2832
|
+
|
|
2833
|
+
// $httpBackend
|
|
2834
|
+
// .expect("POST", "/url")
|
|
2835
|
+
// .respond(200, "resp", { h1: "v1" });
|
|
2836
|
+
// $http
|
|
2837
|
+
// .post("/url", "", { transformResponse: [first, second] })
|
|
2838
|
+
// .then(callback);
|
|
2839
|
+
// $httpBackend.flush();
|
|
2840
|
+
|
|
2841
|
+
// expect(callback).toHaveBeenCalled();
|
|
2842
|
+
// expect(callback.calls.mostRecent().args[0].data).toBe(
|
|
2843
|
+
// "RESP-FIRST:V1",
|
|
2844
|
+
// );
|
|
2845
|
+
// });
|
|
2846
|
+
|
|
2847
|
+
// it("should apply `transformResponse` even if the response data is empty", () => {
|
|
2848
|
+
// const callback = jasmine.createSpy("transformResponse");
|
|
2849
|
+
// const config = { transformResponse: callback };
|
|
2850
|
+
|
|
2851
|
+
// $httpBackend.expect("GET", "/url1").respond(200, undefined);
|
|
2852
|
+
// $httpBackend.expect("GET", "/url2").respond(200, null);
|
|
2853
|
+
// $httpBackend.expect("GET", "/url3").respond(200, "");
|
|
2854
|
+
// $http.get("/url1", config);
|
|
2855
|
+
// $http.get("/url2", config);
|
|
2856
|
+
// $http.get("/url3", config);
|
|
2857
|
+
// $httpBackend.flush();
|
|
2858
|
+
|
|
2859
|
+
// expect(callback).toHaveBeenCalledTimes(3);
|
|
2860
|
+
// expect(callback.calls.argsFor(0)[0]).toBeUndefined();
|
|
2861
|
+
// expect(callback.calls.argsFor(1)[0]).toBe(null);
|
|
2862
|
+
// expect(callback.calls.argsFor(2)[0]).toBe("");
|
|
2863
|
+
// });
|
|
2864
|
+
// });
|
|
2865
|
+
// });
|
|
2866
|
+
|
|
2867
|
+
// describe("cache", () => {
|
|
2868
|
+
// let cache;
|
|
2869
|
+
|
|
2870
|
+
// beforeEach(inject(($cacheFactory) => {
|
|
2871
|
+
// cache = $cacheFactory("testCache");
|
|
2872
|
+
// }));
|
|
2873
|
+
|
|
2874
|
+
// function doFirstCacheRequest(method, respStatus, headers) {
|
|
2875
|
+
// $httpBackend
|
|
2876
|
+
// .expect(method || "GET", "/url")
|
|
2877
|
+
// .respond(respStatus || 200, "content", headers);
|
|
2878
|
+
// await $http({ method: method || "GET", url: "/url", cache }).catch(() => {});
|
|
2879
|
+
// $httpBackend.flush();
|
|
2880
|
+
// }
|
|
2881
|
+
|
|
2882
|
+
// it("should cache GET request when cache is provided", inject((
|
|
2883
|
+
// $rootScope,
|
|
2884
|
+
// ) => {
|
|
2885
|
+
// doFirstCacheRequest();
|
|
2886
|
+
|
|
2887
|
+
// await $http({ method: "get", url: "/url", cache }).then(callback);
|
|
2888
|
+
// $rootScope.$digest();
|
|
2889
|
+
|
|
2890
|
+
// expect(callback).toHaveBeenCalled();
|
|
2891
|
+
// expect(callback.calls.mostRecent().args[0].data).toBe("content");
|
|
2892
|
+
// }));
|
|
2893
|
+
|
|
2894
|
+
// it("should cache JSONP request when cache is provided", inject((
|
|
2895
|
+
// $rootScope,
|
|
2896
|
+
// ) => {
|
|
2897
|
+
// $httpBackend
|
|
2898
|
+
// .expect("JSONP", "/url?callback=JSON_CALLBACK")
|
|
2899
|
+
// .respond("content");
|
|
2900
|
+
// await $http({ method: "JSONP", url: $sce.trustAsResourceUrl("/url"), cache });
|
|
2901
|
+
// $httpBackend.flush();
|
|
2902
|
+
|
|
2903
|
+
// await $http({
|
|
2904
|
+
// method: "JSONP",
|
|
2905
|
+
// url: $sce.trustAsResourceUrl("/url"),
|
|
2906
|
+
// cache,
|
|
2907
|
+
// }).then(callback);
|
|
2908
|
+
// $rootScope.$digest();
|
|
2909
|
+
|
|
2910
|
+
// expect(callback).toHaveBeenCalled();
|
|
2911
|
+
// expect(callback.calls.mostRecent().args[0].data).toBe("content");
|
|
2912
|
+
// }));
|
|
2913
|
+
|
|
2914
|
+
// it("should cache request when cache is provided and no method specified", () => {
|
|
2915
|
+
// doFirstCacheRequest();
|
|
2916
|
+
|
|
2917
|
+
// await $http({ url: "/url", cache }).then(callback);
|
|
2918
|
+
// $rootScope.$digest();
|
|
2919
|
+
|
|
2920
|
+
// expect(callback).toHaveBeenCalled();
|
|
2921
|
+
// expect(callback.calls.mostRecent().args[0].data).toBe("content");
|
|
2922
|
+
// });
|
|
2923
|
+
|
|
2924
|
+
// it("should not cache when cache is not provided", () => {
|
|
2925
|
+
// doFirstCacheRequest();
|
|
2926
|
+
|
|
2927
|
+
// $httpBackend.expect("GET", "/url").respond();
|
|
2928
|
+
// await $http({ method: "GET", url: "/url" });
|
|
2929
|
+
// });
|
|
2930
|
+
|
|
2931
|
+
// it("should perform request when cache cleared", () => {
|
|
2932
|
+
// doFirstCacheRequest();
|
|
2933
|
+
|
|
2934
|
+
// cache.removeAll();
|
|
2935
|
+
// $httpBackend.expect("GET", "/url").respond();
|
|
2936
|
+
// await $http({ method: "GET", url: "/url", cache });
|
|
2937
|
+
// });
|
|
2938
|
+
|
|
2939
|
+
// it("should always call callback asynchronously", () => {
|
|
2940
|
+
// doFirstCacheRequest();
|
|
2941
|
+
// await $http({ method: "get", url: "/url", cache }).then(callback);
|
|
2942
|
+
|
|
2943
|
+
// expect(callback).not.toHaveBeenCalled();
|
|
2944
|
+
// });
|
|
2945
|
+
|
|
2946
|
+
// it("should not cache POST request", () => {
|
|
2947
|
+
// doFirstCacheRequest("POST");
|
|
2948
|
+
|
|
2949
|
+
// $httpBackend.expect("POST", "/url").respond("content2");
|
|
2950
|
+
// await $http({ method: "POST", url: "/url", cache }).then(callback);
|
|
2951
|
+
// $httpBackend.flush();
|
|
2952
|
+
|
|
2953
|
+
// expect(callback).toHaveBeenCalled();
|
|
2954
|
+
// expect(callback.calls.mostRecent().args[0].data).toBe("content2");
|
|
2955
|
+
// });
|
|
2956
|
+
|
|
2957
|
+
// it("should not cache PUT request", () => {
|
|
2958
|
+
// doFirstCacheRequest("PUT");
|
|
2959
|
+
|
|
2960
|
+
// $httpBackend.expect("PUT", "/url").respond("content2");
|
|
2961
|
+
// await $http({ method: "PUT", url: "/url", cache }).then(callback);
|
|
2962
|
+
// $httpBackend.flush();
|
|
2963
|
+
|
|
2964
|
+
// expect(callback).toHaveBeenCalled();
|
|
2965
|
+
// expect(callback.calls.mostRecent().args[0].data).toBe("content2");
|
|
2966
|
+
// });
|
|
2967
|
+
|
|
2968
|
+
// it("should not cache DELETE request", () => {
|
|
2969
|
+
// doFirstCacheRequest("DELETE");
|
|
2970
|
+
|
|
2971
|
+
// $httpBackend.expect("DELETE", "/url").respond(206);
|
|
2972
|
+
// await $http({ method: "DELETE", url: "/url", cache }).then(callback);
|
|
2973
|
+
// $httpBackend.flush();
|
|
2974
|
+
|
|
2975
|
+
// expect(callback).toHaveBeenCalled();
|
|
2976
|
+
// });
|
|
2977
|
+
|
|
2978
|
+
// it("should not cache non 2xx responses", () => {
|
|
2979
|
+
// doFirstCacheRequest("GET", 404);
|
|
2980
|
+
|
|
2981
|
+
// $httpBackend.expect("GET", "/url").respond("content2");
|
|
2982
|
+
// await $http({ method: "GET", url: "/url", cache }).then(callback);
|
|
2983
|
+
// $httpBackend.flush();
|
|
2984
|
+
|
|
2985
|
+
// expect(callback).toHaveBeenCalled();
|
|
2986
|
+
// expect(callback.calls.mostRecent().args[0].data).toBe("content2");
|
|
2987
|
+
// });
|
|
2988
|
+
|
|
2989
|
+
// it("should cache the headers as well", inject(($rootScope) => {
|
|
2990
|
+
// doFirstCacheRequest("GET", 200, {
|
|
2991
|
+
// "content-encoding": "gzip",
|
|
2992
|
+
// server: "Apache",
|
|
2993
|
+
// });
|
|
2994
|
+
// callback.and.callFake((response) => {
|
|
2995
|
+
// expect(response.headers()).toEqual(
|
|
2996
|
+
// extend(Object.create(null), {
|
|
2997
|
+
// "content-encoding": "gzip",
|
|
2998
|
+
// server: "Apache",
|
|
2999
|
+
// }),
|
|
3000
|
+
// );
|
|
3001
|
+
// expect(response.headers("server")).toBe("Apache");
|
|
3002
|
+
// });
|
|
3003
|
+
|
|
3004
|
+
// await $http({ method: "GET", url: "/url", cache }).then(callback);
|
|
3005
|
+
// $rootScope.$digest();
|
|
3006
|
+
// expect(callback).toHaveBeenCalled();
|
|
3007
|
+
// }));
|
|
3008
|
+
|
|
3009
|
+
// it("should not share the cached headers object instance", inject((
|
|
3010
|
+
// $rootScope,
|
|
3011
|
+
// ) => {
|
|
3012
|
+
// doFirstCacheRequest("GET", 200, {
|
|
3013
|
+
// "content-encoding": "gzip",
|
|
3014
|
+
// server: "Apache",
|
|
3015
|
+
// });
|
|
3016
|
+
// callback.and.callFake((response) => {
|
|
3017
|
+
// expect(response.headers()).toEqual(cache.get("/url")[2]);
|
|
3018
|
+
// expect(response.headers()).not.toBe(cache.get("/url")[2]);
|
|
3019
|
+
// });
|
|
3020
|
+
|
|
3021
|
+
// await $http({ method: "GET", url: "/url", cache }).then(callback);
|
|
3022
|
+
// $rootScope.$digest();
|
|
3023
|
+
// expect(callback).toHaveBeenCalled();
|
|
3024
|
+
// }));
|
|
3025
|
+
|
|
3026
|
+
// it("should not share the pending cached headers object instance", inject((
|
|
3027
|
+
// $rootScope,
|
|
3028
|
+
// ) => {
|
|
3029
|
+
// let firstResult;
|
|
3030
|
+
// callback.and.callFake((result) => {
|
|
3031
|
+
// expect(result.headers()).toEqual(firstResult.headers());
|
|
3032
|
+
// expect(result.headers()).not.toBe(firstResult.headers());
|
|
3033
|
+
// });
|
|
3034
|
+
|
|
3035
|
+
// $httpBackend.expect("GET", "/url").respond(200, "content", {
|
|
3036
|
+
// "content-encoding": "gzip",
|
|
3037
|
+
// server: "Apache",
|
|
3038
|
+
// });
|
|
3039
|
+
// await $http({ method: "GET", url: "/url", cache }).then((result) => {
|
|
3040
|
+
// firstResult = result;
|
|
3041
|
+
// });
|
|
3042
|
+
// await $http({ method: "GET", url: "/url", cache }).then(callback);
|
|
3043
|
+
// $httpBackend.flush();
|
|
3044
|
+
|
|
3045
|
+
// expect(callback).toHaveBeenCalled();
|
|
3046
|
+
// }));
|
|
3047
|
+
|
|
3048
|
+
// it("should cache status code as well", inject(($rootScope) => {
|
|
3049
|
+
// doFirstCacheRequest("GET", 201);
|
|
3050
|
+
// callback.and.callFake((response) => {
|
|
3051
|
+
// expect(response.status).toBe(201);
|
|
3052
|
+
// });
|
|
3053
|
+
|
|
3054
|
+
// await $http({ method: "get", url: "/url", cache }).then(callback);
|
|
3055
|
+
// $rootScope.$digest();
|
|
3056
|
+
// expect(callback).toHaveBeenCalled();
|
|
3057
|
+
// }));
|
|
3058
|
+
|
|
3059
|
+
// it("should cache xhrStatus as well", inject(($rootScope) => {
|
|
3060
|
+
// doFirstCacheRequest("GET", 201, null);
|
|
3061
|
+
// callback.and.callFake((response) => {
|
|
3062
|
+
// expect(response.xhrStatus).toBe("complete");
|
|
3063
|
+
// });
|
|
3064
|
+
|
|
3065
|
+
// await $http({ method: "get", url: "/url", cache }).then(callback);
|
|
3066
|
+
// $rootScope.$digest();
|
|
3067
|
+
// expect(callback).toHaveBeenCalled();
|
|
3068
|
+
// }));
|
|
3069
|
+
|
|
3070
|
+
// it("should use cache even if second request was made before the first returned", () => {
|
|
3071
|
+
// $httpBackend.expect("GET", "/url").respond(201, "fake-response");
|
|
3072
|
+
|
|
3073
|
+
// callback.and.callFake((response) => {
|
|
3074
|
+
// expect(response.data).toBe("fake-response");
|
|
3075
|
+
// expect(response.status).toBe(201);
|
|
3076
|
+
// });
|
|
3077
|
+
|
|
3078
|
+
// await $http({ method: "GET", url: "/url", cache }).then(callback);
|
|
3079
|
+
// await $http({ method: "GET", url: "/url", cache }).then(callback);
|
|
3080
|
+
|
|
3081
|
+
// $httpBackend.flush();
|
|
3082
|
+
// expect(callback).toHaveBeenCalled();
|
|
3083
|
+
// expect(callback).toHaveBeenCalledTimes(2);
|
|
3084
|
+
// });
|
|
3085
|
+
|
|
3086
|
+
// it("should preserve config object when resolving from cache", () => {
|
|
3087
|
+
// $httpBackend.expect("GET", "/url").respond(200, "content");
|
|
3088
|
+
// await $http({ method: "GET", url: "/url", cache, headers: { foo: "bar" } });
|
|
3089
|
+
// $httpBackend.flush();
|
|
3090
|
+
|
|
3091
|
+
// await $http({
|
|
3092
|
+
// method: "GET",
|
|
3093
|
+
// url: "/url",
|
|
3094
|
+
// cache,
|
|
3095
|
+
// headers: { foo: "baz" },
|
|
3096
|
+
// }).then(callback);
|
|
3097
|
+
// $rootScope.$digest();
|
|
3098
|
+
|
|
3099
|
+
// expect(callback.calls.mostRecent().args[0].config.headers.foo).toBe(
|
|
3100
|
+
// "baz",
|
|
3101
|
+
// );
|
|
3102
|
+
// });
|
|
3103
|
+
|
|
3104
|
+
// it("should preserve config object when resolving from pending cache", () => {
|
|
3105
|
+
// $httpBackend.expect("GET", "/url").respond(200, "content");
|
|
3106
|
+
// await $http({ method: "GET", url: "/url", cache, headers: { foo: "bar" } });
|
|
3107
|
+
|
|
3108
|
+
// await $http({
|
|
3109
|
+
// method: "GET",
|
|
3110
|
+
// url: "/url",
|
|
3111
|
+
// cache,
|
|
3112
|
+
// headers: { foo: "baz" },
|
|
3113
|
+
// }).then(callback);
|
|
3114
|
+
// $httpBackend.flush();
|
|
3115
|
+
|
|
3116
|
+
// expect(callback.calls.mostRecent().args[0].config.headers.foo).toBe(
|
|
3117
|
+
// "baz",
|
|
3118
|
+
// );
|
|
3119
|
+
// });
|
|
3120
|
+
|
|
3121
|
+
// it("should preserve config object when rejecting from pending cache", () => {
|
|
3122
|
+
// $httpBackend.expect("GET", "/url").respond(404, "content");
|
|
3123
|
+
// await $http({
|
|
3124
|
+
// method: "GET",
|
|
3125
|
+
// url: "/url",
|
|
3126
|
+
// cache,
|
|
3127
|
+
// headers: { foo: "bar" },
|
|
3128
|
+
// }).catch(() => {});
|
|
3129
|
+
|
|
3130
|
+
// await $http({
|
|
3131
|
+
// method: "GET",
|
|
3132
|
+
// url: "/url",
|
|
3133
|
+
// cache,
|
|
3134
|
+
// headers: { foo: "baz" },
|
|
3135
|
+
// }).catch(callback);
|
|
3136
|
+
// $httpBackend.flush();
|
|
3137
|
+
|
|
3138
|
+
// expect(callback.calls.mostRecent().args[0].config.headers.foo).toBe(
|
|
3139
|
+
// "baz",
|
|
3140
|
+
// );
|
|
3141
|
+
// });
|
|
3142
|
+
|
|
3143
|
+
// it("should allow the cached value to be an empty string", () => {
|
|
3144
|
+
// cache.put("/abc", "");
|
|
3145
|
+
|
|
3146
|
+
// callback.and.callFake((response) => {
|
|
3147
|
+
// expect(response.data).toBe("");
|
|
3148
|
+
// expect(response.status).toBe(200);
|
|
3149
|
+
// });
|
|
3150
|
+
|
|
3151
|
+
// await $http({ method: "GET", url: "/abc", cache }).then(callback);
|
|
3152
|
+
// $rootScope.$digest();
|
|
3153
|
+
// expect(callback).toHaveBeenCalled();
|
|
3154
|
+
// });
|
|
3155
|
+
|
|
3156
|
+
// it("should default to status code 200 and empty headers if cache contains a non-array element", inject((
|
|
3157
|
+
// $rootScope,
|
|
3158
|
+
// ) => {
|
|
3159
|
+
// cache.put("/myurl", "simple response");
|
|
3160
|
+
// $http.get("/myurl", { cache }).then((response) => {
|
|
3161
|
+
// expect(response.data).toBe("simple response");
|
|
3162
|
+
// expect(response.status).toBe(200);
|
|
3163
|
+
// expect(response.headers()).toEqual(Object.create(null));
|
|
3164
|
+
// callback();
|
|
3165
|
+
// });
|
|
3166
|
+
|
|
3167
|
+
// $rootScope.$digest();
|
|
3168
|
+
// expect(callback).toHaveBeenCalled();
|
|
3169
|
+
// }));
|
|
3170
|
+
|
|
3171
|
+
// describe("$http.defaults.cache", () => {
|
|
3172
|
+
// it("should be undefined by default", () => {
|
|
3173
|
+
// expect($http.defaults.cache).toBeUndefined();
|
|
3174
|
+
// });
|
|
3175
|
+
|
|
3176
|
+
// it("should cache requests when no cache given in request config", () => {
|
|
3177
|
+
// $http.defaults.cache = cache;
|
|
3178
|
+
|
|
3179
|
+
// // First request fills the cache from server response.
|
|
3180
|
+
// $httpBackend.expect("GET", "/url").respond(200, "content");
|
|
3181
|
+
// await $http({ method: "GET", url: "/url" }); // Notice no cache given in config.
|
|
3182
|
+
// $httpBackend.flush();
|
|
3183
|
+
|
|
3184
|
+
// // Second should be served from cache, without sending request to server.
|
|
3185
|
+
// await $http({ method: "get", url: "/url" }).then(callback);
|
|
3186
|
+
// $rootScope.$digest();
|
|
3187
|
+
|
|
3188
|
+
// expect(callback).toHaveBeenCalled();
|
|
3189
|
+
// expect(callback.calls.mostRecent().args[0].data).toBe("content");
|
|
3190
|
+
|
|
3191
|
+
// // Invalidate cache entry.
|
|
3192
|
+
// $http.defaults.cache.remove("/url");
|
|
3193
|
+
|
|
3194
|
+
// // After cache entry removed, a request should be sent to server.
|
|
3195
|
+
// $httpBackend.expect("GET", "/url").respond(200, "content");
|
|
3196
|
+
// await $http({ method: "GET", url: "/url" });
|
|
3197
|
+
// $httpBackend.flush();
|
|
3198
|
+
// });
|
|
3199
|
+
|
|
3200
|
+
// it("should have less priority than explicitly given cache", inject((
|
|
3201
|
+
// $cacheFactory,
|
|
3202
|
+
// ) => {
|
|
3203
|
+
// const localCache = $cacheFactory("localCache");
|
|
3204
|
+
// $http.defaults.cache = cache;
|
|
3205
|
+
|
|
3206
|
+
// // Fill local cache.
|
|
3207
|
+
// $httpBackend
|
|
3208
|
+
// .expect("GET", "/url")
|
|
3209
|
+
// .respond(200, "content-local-cache");
|
|
3210
|
+
// await $http({ method: "GET", url: "/url", cache: localCache });
|
|
3211
|
+
// $httpBackend.flush();
|
|
3212
|
+
|
|
3213
|
+
// // Fill default cache.
|
|
3214
|
+
// $httpBackend
|
|
3215
|
+
// .expect("GET", "/url")
|
|
3216
|
+
// .respond(200, "content-default-cache");
|
|
3217
|
+
// await $http({ method: "GET", url: "/url" });
|
|
3218
|
+
// $httpBackend.flush();
|
|
3219
|
+
|
|
3220
|
+
// // Serve request from default cache when no local given.
|
|
3221
|
+
// await $http({ method: "get", url: "/url" }).then(callback);
|
|
3222
|
+
// $rootScope.$digest();
|
|
3223
|
+
// expect(callback).toHaveBeenCalled();
|
|
3224
|
+
// expect(callback.calls.mostRecent().args[0].data).toBe(
|
|
3225
|
+
// "content-default-cache",
|
|
3226
|
+
// );
|
|
3227
|
+
// callback.calls.reset();
|
|
3228
|
+
|
|
3229
|
+
// // Serve request from local cache when it is given (but default filled too).
|
|
3230
|
+
// await $http({ method: "get", url: "/url", cache: localCache }).then(
|
|
3231
|
+
// callback,
|
|
3232
|
+
// );
|
|
3233
|
+
// $rootScope.$digest();
|
|
3234
|
+
// expect(callback).toHaveBeenCalled();
|
|
3235
|
+
// expect(callback.calls.mostRecent().args[0].data).toBe(
|
|
3236
|
+
// "content-local-cache",
|
|
3237
|
+
// );
|
|
3238
|
+
// }));
|
|
3239
|
+
|
|
3240
|
+
// it("should be skipped if {cache: false} is passed in request config", () => {
|
|
3241
|
+
// $http.defaults.cache = cache;
|
|
3242
|
+
|
|
3243
|
+
// $httpBackend.expect("GET", "/url").respond(200, "content");
|
|
3244
|
+
// await $http({ method: "GET", url: "/url" });
|
|
3245
|
+
// $httpBackend.flush();
|
|
3246
|
+
|
|
3247
|
+
// $httpBackend.expect("GET", "/url").respond();
|
|
3248
|
+
// await $http({ method: "GET", url: "/url", cache: false });
|
|
3249
|
+
// $httpBackend.flush();
|
|
3250
|
+
// });
|
|
3251
|
+
// });
|
|
3252
|
+
// });
|
|
3253
|
+
|
|
3254
|
+
// describe("timeout", () => {
|
|
3255
|
+
// it("should abort requests when timeout promise resolves", inject(($q) => {
|
|
3256
|
+
// const canceler = $q.defer();
|
|
3257
|
+
|
|
3258
|
+
// $httpBackend.expect("GET", "/some").respond(200);
|
|
3259
|
+
|
|
3260
|
+
// await $http({ method: "GET", url: "/some", timeout: canceler.promise }).catch(
|
|
3261
|
+
// (response) => {
|
|
3262
|
+
// expect(response.data).toBeUndefined();
|
|
3263
|
+
// expect(response.status).toBe(-1);
|
|
3264
|
+
// expect(response.xhrStatus).toBe("abort");
|
|
3265
|
+
// expect(response.headers()).toEqual(Object.create(null));
|
|
3266
|
+
// expect(response.config.url).toBe("/some");
|
|
3267
|
+
// callback();
|
|
3268
|
+
// },
|
|
3269
|
+
// );
|
|
3270
|
+
|
|
3271
|
+
// $rootScope.$apply(() => {
|
|
3272
|
+
// canceler.resolve();
|
|
3273
|
+
// });
|
|
3274
|
+
|
|
3275
|
+
// expect(callback).toHaveBeenCalled();
|
|
3276
|
+
// $httpBackend.verifyNoOutstandingExpectation();
|
|
3277
|
+
// $httpBackend.verifyNoOutstandingRequest();
|
|
3278
|
+
// }));
|
|
3279
|
+
|
|
3280
|
+
// it("should timeout request when numerical timeout is exceeded", inject((
|
|
3281
|
+
// $timeout,
|
|
3282
|
+
// ) => {
|
|
3283
|
+
// const onFulfilled = jasmine.createSpy("onFulfilled");
|
|
3284
|
+
// const onRejected = jasmine
|
|
3285
|
+
// .createSpy("onRejected")
|
|
3286
|
+
// .and.callFake((response) => {
|
|
3287
|
+
// expect(response.xhrStatus).toBe("timeout");
|
|
3288
|
+
// });
|
|
3289
|
+
|
|
3290
|
+
// $httpBackend.expect("GET", "/some").respond(200);
|
|
3291
|
+
|
|
3292
|
+
// await $http({
|
|
3293
|
+
// method: "GET",
|
|
3294
|
+
// url: "/some",
|
|
3295
|
+
// timeout: 10,
|
|
3296
|
+
// }).then(onFulfilled, onRejected);
|
|
3297
|
+
|
|
3298
|
+
// $timeout.flush(100);
|
|
3299
|
+
|
|
3300
|
+
// expect(onFulfilled).not.toHaveBeenCalled();
|
|
3301
|
+
// expect(onRejected).toHaveBeenCalled();
|
|
3302
|
+
// }));
|
|
3303
|
+
|
|
3304
|
+
// it("should reject promise when timeout promise resolves", inject((
|
|
3305
|
+
// $timeout,
|
|
3306
|
+
// ) => {
|
|
3307
|
+
// const onFulfilled = jasmine.createSpy("onFulfilled");
|
|
3308
|
+
// const onRejected = jasmine
|
|
3309
|
+
// .createSpy("onRejected")
|
|
3310
|
+
// .and.callFake((response) => {
|
|
3311
|
+
// expect(response.xhrStatus).toBe("timeout");
|
|
3312
|
+
// });
|
|
3313
|
+
|
|
3314
|
+
// $httpBackend.expect("GET", "/some").respond(200);
|
|
3315
|
+
|
|
3316
|
+
// await $http({
|
|
3317
|
+
// method: "GET",
|
|
3318
|
+
// url: "/some",
|
|
3319
|
+
// timeout: $timeout(() => {}, 10),
|
|
3320
|
+
// }).then(onFulfilled, onRejected);
|
|
3321
|
+
|
|
3322
|
+
// $timeout.flush(100);
|
|
3323
|
+
|
|
3324
|
+
// expect(onFulfilled).not.toHaveBeenCalled();
|
|
3325
|
+
// expect(onRejected).toHaveBeenCalled();
|
|
3326
|
+
// }));
|
|
3327
|
+
// });
|
|
3328
|
+
|
|
3329
|
+
// describe("pendingRequests", () => {
|
|
3330
|
+
// it("should be an array of pending requests", () => {
|
|
3331
|
+
// $httpBackend.when("GET").respond(200);
|
|
3332
|
+
// expect($http.pendingRequests.length).toBe(0);
|
|
3333
|
+
|
|
3334
|
+
// await $http({ method: "get", url: "/some" });
|
|
3335
|
+
// $rootScope.$digest();
|
|
3336
|
+
// expect($http.pendingRequests.length).toBe(1);
|
|
3337
|
+
|
|
3338
|
+
// $httpBackend.flush();
|
|
3339
|
+
// expect($http.pendingRequests.length).toBe(0);
|
|
3340
|
+
// });
|
|
3341
|
+
|
|
3342
|
+
// it("should update pending requests even when served from cache", inject((
|
|
3343
|
+
// $rootScope,
|
|
3344
|
+
// ) => {
|
|
3345
|
+
// $httpBackend.when("GET").respond(200);
|
|
3346
|
+
|
|
3347
|
+
// await $http({ method: "get", url: "/cached", cache: true });
|
|
3348
|
+
// await $http({ method: "get", url: "/cached", cache: true });
|
|
3349
|
+
// $rootScope.$digest();
|
|
3350
|
+
// expect($http.pendingRequests.length).toBe(2);
|
|
3351
|
+
|
|
3352
|
+
// $httpBackend.flush();
|
|
3353
|
+
// expect($http.pendingRequests.length).toBe(0);
|
|
3354
|
+
|
|
3355
|
+
// await $http({ method: "get", url: "/cached", cache: true });
|
|
3356
|
+
// spyOn($http.pendingRequests, "push").and.callThrough();
|
|
3357
|
+
// $rootScope.$digest();
|
|
3358
|
+
// expect($http.pendingRequests.push).toHaveBeenCalled();
|
|
3359
|
+
|
|
3360
|
+
// $rootScope.$apply();
|
|
3361
|
+
// expect($http.pendingRequests.length).toBe(0);
|
|
3362
|
+
// }));
|
|
3363
|
+
|
|
3364
|
+
// it("should remove the request before firing callbacks", () => {
|
|
3365
|
+
// $httpBackend.when("GET").respond(200);
|
|
3366
|
+
// await $http({ method: "get", url: "/url" }).then(() => {
|
|
3367
|
+
// expect($http.pendingRequests.length).toBe(0);
|
|
3368
|
+
// });
|
|
3369
|
+
|
|
3370
|
+
// $rootScope.$digest();
|
|
3371
|
+
// expect($http.pendingRequests.length).toBe(1);
|
|
3372
|
+
// $httpBackend.flush();
|
|
3373
|
+
// });
|
|
3374
|
+
// });
|
|
3375
|
+
|
|
3376
|
+
// describe("defaults", () => {
|
|
3377
|
+
// it("should expose the defaults object at runtime", () => {
|
|
3378
|
+
// expect($http.defaults).toBeDefined();
|
|
3379
|
+
|
|
3380
|
+
// $http.defaults.headers.common.foo = "bar";
|
|
3381
|
+
// $httpBackend
|
|
3382
|
+
// .expect("GET", "/url", undefined, (headers) => headers.foo === "bar")
|
|
3383
|
+
// .respond("");
|
|
3384
|
+
|
|
3385
|
+
// $http.get("/url");
|
|
3386
|
+
// $httpBackend.flush();
|
|
3387
|
+
// });
|
|
3388
|
+
|
|
3389
|
+
// it("should have separate objects for defaults PUT and POST", () => {
|
|
3390
|
+
// expect($http.defaults.headers.post).not.toBe(
|
|
3391
|
+
// $http.defaults.headers.put,
|
|
3392
|
+
// );
|
|
3393
|
+
// expect($http.defaults.headers.post).not.toBe(
|
|
3394
|
+
// $http.defaults.headers.patch,
|
|
3395
|
+
// );
|
|
3396
|
+
// expect($http.defaults.headers.put).not.toBe(
|
|
3397
|
+
// $http.defaults.headers.patch,
|
|
3398
|
+
// );
|
|
3399
|
+
// });
|
|
3400
|
+
|
|
3401
|
+
// it("should expose default param serializer at runtime", () => {
|
|
3402
|
+
// const { paramSerializer } = $http.defaults;
|
|
3403
|
+
// expect(paramSerializer({ foo: "foo", bar: ["bar", "baz"] })).toEqual(
|
|
3404
|
+
// "bar=bar&bar=baz&foo=foo",
|
|
3405
|
+
// );
|
|
3406
|
+
// });
|
|
3407
|
+
// });
|
|
3408
|
+
// });
|
|
3409
|
+
|
|
3410
|
+
// describe("$browser's outstandingRequestCount", () => {
|
|
3411
|
+
// let $http;
|
|
3412
|
+
// let $httpBackend;
|
|
3413
|
+
// let $rootScope;
|
|
3414
|
+
// let incOutstandingRequestCountSpy;
|
|
3415
|
+
// let completeOutstandingRequestSpy;
|
|
3416
|
+
|
|
3417
|
+
// describe("without interceptors", () => {
|
|
3418
|
+
// beforeEach(setupServicesAndSpies);
|
|
3419
|
+
|
|
3420
|
+
// it("should immediately call `$browser.$$incOutstandingRequestCount()`", () => {
|
|
3421
|
+
// expect(incOutstandingRequestCountSpy).not.toHaveBeenCalled();
|
|
3422
|
+
// $http.get("");
|
|
3423
|
+
// expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnceWith("$http");
|
|
3424
|
+
// });
|
|
3425
|
+
|
|
3426
|
+
// it("should call `$browser.$$completeOutstandingRequest()` on success", () => {
|
|
3427
|
+
// $httpBackend.when("GET").respond(200);
|
|
3428
|
+
|
|
3429
|
+
// $http.get("");
|
|
3430
|
+
// expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
|
|
3431
|
+
// $httpBackend.flush();
|
|
3432
|
+
// expect(completeOutstandingRequestSpy).toHaveBeenCalledOnceWith(
|
|
3433
|
+
// () => {},
|
|
3434
|
+
// "$http",
|
|
3435
|
+
// );
|
|
3436
|
+
// });
|
|
3437
|
+
|
|
3438
|
+
// it("should call `$browser.$$completeOutstandingRequest()` on error", () => {
|
|
3439
|
+
// $httpBackend.when("GET").respond(500);
|
|
3440
|
+
|
|
3441
|
+
// $http.get("").catch(() => {});
|
|
3442
|
+
// expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
|
|
3443
|
+
// $httpBackend.flush();
|
|
3444
|
+
// expect(completeOutstandingRequestSpy).toHaveBeenCalledOnceWith(
|
|
3445
|
+
// () => {},
|
|
3446
|
+
// "$http",
|
|
3447
|
+
// );
|
|
3448
|
+
// });
|
|
3449
|
+
|
|
3450
|
+
// it("should increment/decrement `outstandingRequestCount` on error in `transformRequest`", () => {
|
|
3451
|
+
// expect(incOutstandingRequestCountSpy).not.toHaveBeenCalled();
|
|
3452
|
+
// expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
|
|
3453
|
+
|
|
3454
|
+
// $http
|
|
3455
|
+
// .get("", {
|
|
3456
|
+
// transformRequest() {
|
|
3457
|
+
// throw new Error();
|
|
3458
|
+
// },
|
|
3459
|
+
// })
|
|
3460
|
+
// .catch(() => {});
|
|
3461
|
+
|
|
3462
|
+
// expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnceWith("$http");
|
|
3463
|
+
// expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
|
|
3464
|
+
|
|
3465
|
+
// $rootScope.$digest();
|
|
3466
|
+
|
|
3467
|
+
// expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnceWith("$http");
|
|
3468
|
+
// expect(completeOutstandingRequestSpy).toHaveBeenCalledOnceWith(
|
|
3469
|
+
// () => {},
|
|
3470
|
+
// "$http",
|
|
3471
|
+
// );
|
|
3472
|
+
// });
|
|
3473
|
+
|
|
3474
|
+
// it("should increment/decrement `outstandingRequestCount` on error in `transformResponse`", () => {
|
|
3475
|
+
// expect(incOutstandingRequestCountSpy).not.toHaveBeenCalled();
|
|
3476
|
+
// expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
|
|
3477
|
+
|
|
3478
|
+
// $httpBackend.when("GET").respond(200);
|
|
3479
|
+
// $http
|
|
3480
|
+
// .get("", {
|
|
3481
|
+
// transformResponse() {
|
|
3482
|
+
// throw new Error();
|
|
3483
|
+
// },
|
|
3484
|
+
// })
|
|
3485
|
+
// .catch(() => {});
|
|
3486
|
+
|
|
3487
|
+
// expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnceWith("$http");
|
|
3488
|
+
// expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
|
|
3489
|
+
|
|
3490
|
+
// $httpBackend.flush();
|
|
3491
|
+
|
|
3492
|
+
// expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnceWith("$http");
|
|
3493
|
+
// expect(completeOutstandingRequestSpy).toHaveBeenCalledOnceWith(
|
|
3494
|
+
// () => {},
|
|
3495
|
+
// "$http",
|
|
3496
|
+
// );
|
|
3497
|
+
// });
|
|
3498
|
+
// });
|
|
3499
|
+
|
|
3500
|
+
// describe("with interceptors", () => {
|
|
3501
|
+
// let reqInterceptorDeferred;
|
|
3502
|
+
// let resInterceptorDeferred;
|
|
3503
|
+
// let reqInterceptorFulfilled;
|
|
3504
|
+
// let resInterceptorFulfilled;
|
|
3505
|
+
|
|
3506
|
+
// beforeEach(
|
|
3507
|
+
// module(($httpProvider) => {
|
|
3508
|
+
// reqInterceptorDeferred = null;
|
|
3509
|
+
// resInterceptorDeferred = null;
|
|
3510
|
+
// reqInterceptorFulfilled = false;
|
|
3511
|
+
// resInterceptorFulfilled = false;
|
|
3512
|
+
|
|
3513
|
+
// $httpProvider.interceptors.push(($q) => ({
|
|
3514
|
+
// request(config) {
|
|
3515
|
+
// return (reqInterceptorDeferred = $q.defer()).promise
|
|
3516
|
+
// .finally(() => {
|
|
3517
|
+
// reqInterceptorFulfilled = true;
|
|
3518
|
+
// })
|
|
3519
|
+
// .then(valueFn(config));
|
|
3520
|
+
// },
|
|
3521
|
+
// response() {
|
|
3522
|
+
// return (resInterceptorDeferred = $q.defer()).promise.finally(
|
|
3523
|
+
// () => {
|
|
3524
|
+
// resInterceptorFulfilled = true;
|
|
3525
|
+
// },
|
|
3526
|
+
// );
|
|
3527
|
+
// },
|
|
3528
|
+
// }));
|
|
3529
|
+
// }),
|
|
3530
|
+
// );
|
|
3531
|
+
|
|
3532
|
+
// beforeEach(setupServicesAndSpies);
|
|
3533
|
+
|
|
3534
|
+
// beforeEach(() => {
|
|
3535
|
+
// $httpBackend.when("GET").respond(200);
|
|
3536
|
+
// });
|
|
3537
|
+
|
|
3538
|
+
// it("should increment/decrement `outstandingRequestCount` before/after async interceptors", () => {
|
|
3539
|
+
// expect(reqInterceptorFulfilled).toBe(false);
|
|
3540
|
+
// expect(resInterceptorFulfilled).toBe(false);
|
|
3541
|
+
// expect(incOutstandingRequestCountSpy).not.toHaveBeenCalled();
|
|
3542
|
+
// expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
|
|
3543
|
+
|
|
3544
|
+
// $http.get("");
|
|
3545
|
+
// $rootScope.$digest();
|
|
3546
|
+
|
|
3547
|
+
// expect(reqInterceptorFulfilled).toBe(false);
|
|
3548
|
+
// expect(resInterceptorFulfilled).toBe(false);
|
|
3549
|
+
// expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnceWith("$http");
|
|
3550
|
+
// expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
|
|
3551
|
+
|
|
3552
|
+
// reqInterceptorDeferred.resolve();
|
|
3553
|
+
// $httpBackend.flush();
|
|
3554
|
+
|
|
3555
|
+
// expect(reqInterceptorFulfilled).toBe(true);
|
|
3556
|
+
// expect(resInterceptorFulfilled).toBe(false);
|
|
3557
|
+
// expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnceWith("$http");
|
|
3558
|
+
// expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
|
|
3559
|
+
|
|
3560
|
+
// resInterceptorDeferred.resolve();
|
|
3561
|
+
// $rootScope.$digest();
|
|
3562
|
+
|
|
3563
|
+
// expect(reqInterceptorFulfilled).toBe(true);
|
|
3564
|
+
// expect(resInterceptorFulfilled).toBe(true);
|
|
3565
|
+
// expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnceWith("$http");
|
|
3566
|
+
// expect(completeOutstandingRequestSpy).toHaveBeenCalledOnceWith(
|
|
3567
|
+
// () => {},
|
|
3568
|
+
// "$http",
|
|
3569
|
+
// );
|
|
3570
|
+
// });
|
|
3571
|
+
|
|
3572
|
+
// it("should increment/decrement `outstandingRequestCount` on error in request interceptor", () => {
|
|
3573
|
+
// expect(reqInterceptorFulfilled).toBe(false);
|
|
3574
|
+
// expect(incOutstandingRequestCountSpy).not.toHaveBeenCalled();
|
|
3575
|
+
// expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
|
|
3576
|
+
|
|
3577
|
+
// $http.get("").catch(() => {});
|
|
3578
|
+
// $rootScope.$digest();
|
|
3579
|
+
|
|
3580
|
+
// expect(reqInterceptorFulfilled).toBe(false);
|
|
3581
|
+
// expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnceWith("$http");
|
|
3582
|
+
// expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
|
|
3583
|
+
|
|
3584
|
+
// reqInterceptorDeferred.reject();
|
|
3585
|
+
// $rootScope.$digest();
|
|
3586
|
+
|
|
3587
|
+
// expect(reqInterceptorFulfilled).toBe(true);
|
|
3588
|
+
// expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnceWith("$http");
|
|
3589
|
+
// expect(completeOutstandingRequestSpy).toHaveBeenCalledOnceWith(
|
|
3590
|
+
// () => {},
|
|
3591
|
+
// "$http",
|
|
3592
|
+
// );
|
|
3593
|
+
// });
|
|
3594
|
+
|
|
3595
|
+
// it("should increment/decrement `outstandingRequestCount` on error in response interceptor", () => {
|
|
3596
|
+
// expect(reqInterceptorFulfilled).toBe(false);
|
|
3597
|
+
// expect(resInterceptorFulfilled).toBe(false);
|
|
3598
|
+
// expect(incOutstandingRequestCountSpy).not.toHaveBeenCalled();
|
|
3599
|
+
// expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
|
|
3600
|
+
|
|
3601
|
+
// $http.get("").catch(() => {});
|
|
3602
|
+
// $rootScope.$digest();
|
|
3603
|
+
|
|
3604
|
+
// expect(reqInterceptorFulfilled).toBe(false);
|
|
3605
|
+
// expect(resInterceptorFulfilled).toBe(false);
|
|
3606
|
+
// expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnceWith("$http");
|
|
3607
|
+
// expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
|
|
3608
|
+
|
|
3609
|
+
// reqInterceptorDeferred.resolve();
|
|
3610
|
+
// $httpBackend.flush();
|
|
3611
|
+
|
|
3612
|
+
// expect(reqInterceptorFulfilled).toBe(true);
|
|
3613
|
+
// expect(resInterceptorFulfilled).toBe(false);
|
|
3614
|
+
// expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnceWith("$http");
|
|
3615
|
+
// expect(completeOutstandingRequestSpy).not.toHaveBeenCalled();
|
|
3616
|
+
|
|
3617
|
+
// resInterceptorDeferred.reject();
|
|
3618
|
+
// $rootScope.$digest();
|
|
3619
|
+
|
|
3620
|
+
// expect(reqInterceptorFulfilled).toBe(true);
|
|
3621
|
+
// expect(resInterceptorFulfilled).toBe(true);
|
|
3622
|
+
// expect(incOutstandingRequestCountSpy).toHaveBeenCalledOnceWith("$http");
|
|
3623
|
+
// expect(completeOutstandingRequestSpy).toHaveBeenCalledOnceWith(
|
|
3624
|
+
// () => {},
|
|
3625
|
+
// "$http",
|
|
3626
|
+
// );
|
|
3627
|
+
// });
|
|
3628
|
+
// });
|
|
3629
|
+
|
|
3630
|
+
// // Helpers
|
|
3631
|
+
// function setupServicesAndSpies() {
|
|
3632
|
+
// inject(($browser, _$http_, _$httpBackend_, _$rootScope_) => {
|
|
3633
|
+
// $http = _$http_;
|
|
3634
|
+
// $httpBackend = _$httpBackend_;
|
|
3635
|
+
// $rootScope = _$rootScope_;
|
|
3636
|
+
|
|
3637
|
+
// incOutstandingRequestCountSpy = spyOn(
|
|
3638
|
+
// $browser,
|
|
3639
|
+
// "$$incOutstandingRequestCount",
|
|
3640
|
+
// ).and.callThrough();
|
|
3641
|
+
// completeOutstandingRequestSpy = spyOn(
|
|
3642
|
+
// $browser,
|
|
3643
|
+
// "$$completeOutstandingRequest",
|
|
3644
|
+
// ).and.callThrough();
|
|
3645
|
+
// });
|
|
3646
|
+
// }
|
|
3647
|
+
// });
|
|
3648
|
+
|
|
3649
|
+
// describe("XSRF", () => {
|
|
3650
|
+
// let $http;
|
|
3651
|
+
// let $httpBackend;
|
|
3652
|
+
|
|
3653
|
+
// beforeEach(
|
|
3654
|
+
// module(($httpProvider) => {
|
|
3655
|
+
// $httpProvider.xsrfTrustedOrigins.push(
|
|
3656
|
+
// "https://trusted.example.com",
|
|
3657
|
+
// "https://trusted2.example.com:1337/ignored/path",
|
|
3658
|
+
// );
|
|
3659
|
+
// }),
|
|
3660
|
+
// );
|
|
3661
|
+
|
|
3662
|
+
// beforeEach(inject((_$http_, _$httpBackend_) => {
|
|
3663
|
+
// $http = _$http_;
|
|
3664
|
+
// $httpBackend = _$httpBackend_;
|
|
3665
|
+
// }));
|
|
3666
|
+
|
|
3667
|
+
// it("should set the XSRF cookie into an XSRF header", () => {
|
|
3668
|
+
// function checkXsrf(secret, header) {
|
|
3669
|
+
// return function checkHeaders(headers) {
|
|
3670
|
+
// return headers[header || "X-XSRF-TOKEN"] === secret;
|
|
3671
|
+
// };
|
|
3672
|
+
// }
|
|
3673
|
+
|
|
3674
|
+
// mockedCookies["XSRF-TOKEN"] = "secret";
|
|
3675
|
+
// mockedCookies.aCookie = "secret2";
|
|
3676
|
+
// $httpBackend
|
|
3677
|
+
// .expect("GET", "/url", null, checkXsrf("secret"))
|
|
3678
|
+
// .respond(null);
|
|
3679
|
+
// $httpBackend
|
|
3680
|
+
// .expect("POST", "/url", null, checkXsrf("secret"))
|
|
3681
|
+
// .respond(null);
|
|
3682
|
+
// $httpBackend
|
|
3683
|
+
// .expect("PUT", "/url", null, checkXsrf("secret"))
|
|
3684
|
+
// .respond(null);
|
|
3685
|
+
// $httpBackend
|
|
3686
|
+
// .expect("DELETE", "/url", null, checkXsrf("secret"))
|
|
3687
|
+
// .respond(null);
|
|
3688
|
+
// $httpBackend
|
|
3689
|
+
// .expect("GET", "/url", null, checkXsrf("secret", "aHeader"))
|
|
3690
|
+
// .respond(null);
|
|
3691
|
+
// $httpBackend
|
|
3692
|
+
// .expect("GET", "/url", null, checkXsrf("secret2"))
|
|
3693
|
+
// .respond(null);
|
|
3694
|
+
|
|
3695
|
+
// await $http({ method: "GET", url: "/url" });
|
|
3696
|
+
// await $http({ method: "POST", url: "/url", headers: { "S-ome": "Header" } });
|
|
3697
|
+
// await $http({ method: "PUT", url: "/url", headers: { Another: "Header" } });
|
|
3698
|
+
// await $http({ method: "DELETE", url: "/url", headers: {} });
|
|
3699
|
+
// await $http({ method: "GET", url: "/url", xsrfHeaderName: "aHeader" });
|
|
3700
|
+
// await $http({ method: "GET", url: "/url", xsrfCookieName: "aCookie" });
|
|
3701
|
+
|
|
3702
|
+
// $httpBackend.flush();
|
|
3703
|
+
// });
|
|
3704
|
+
|
|
3705
|
+
// it("should support setting a default XSRF cookie/header name", () => {
|
|
3706
|
+
// $http.defaults.xsrfCookieName = "aCookie";
|
|
3707
|
+
// $http.defaults.xsrfHeaderName = "aHeader";
|
|
3708
|
+
|
|
3709
|
+
// function checkHeaders(headers) {
|
|
3710
|
+
// return headers.aHeader === "secret";
|
|
3711
|
+
// }
|
|
3712
|
+
|
|
3713
|
+
// mockedCookies.aCookie = "secret";
|
|
3714
|
+
// $httpBackend.expect("GET", "/url", null, checkHeaders).respond(null);
|
|
3715
|
+
|
|
3716
|
+
// $http.get("/url");
|
|
3717
|
+
|
|
3718
|
+
// $httpBackend.flush();
|
|
3719
|
+
// });
|
|
3720
|
+
|
|
3721
|
+
// it("should support overriding the default XSRF cookie/header name per request", () => {
|
|
3722
|
+
// $http.defaults.xsrfCookieName = "aCookie";
|
|
3723
|
+
// $http.defaults.xsrfHeaderName = "aHeader";
|
|
3724
|
+
|
|
3725
|
+
// function checkHeaders(headers) {
|
|
3726
|
+
// return headers.anotherHeader === "anotherSecret";
|
|
3727
|
+
// }
|
|
3728
|
+
|
|
3729
|
+
// mockedCookies.anotherCookie = "anotherSecret";
|
|
3730
|
+
// $httpBackend.expect("GET", "/url", null, checkHeaders).respond(null);
|
|
3731
|
+
|
|
3732
|
+
// $http.get("/url", {
|
|
3733
|
+
// xsrfCookieName: "anotherCookie",
|
|
3734
|
+
// xsrfHeaderName: "anotherHeader",
|
|
3735
|
+
// });
|
|
3736
|
+
|
|
3737
|
+
// $httpBackend.flush();
|
|
3738
|
+
// });
|
|
3739
|
+
|
|
3740
|
+
// it("should check the cache before checking the XSRF cookie", inject((
|
|
3741
|
+
// $cacheFactory,
|
|
3742
|
+
// ) => {
|
|
3743
|
+
// function checkHeaders(headers) {
|
|
3744
|
+
// return headers["X-XSRF-TOKEN"] === "foo";
|
|
3745
|
+
// }
|
|
3746
|
+
// function setCookie() {
|
|
3747
|
+
// mockedCookies["XSRF-TOKEN"] = "foo";
|
|
3748
|
+
// }
|
|
3749
|
+
|
|
3750
|
+
// const testCache = $cacheFactory("testCache");
|
|
3751
|
+
// spyOn(testCache, "get").and.callFake(setCookie);
|
|
3752
|
+
|
|
3753
|
+
// $httpBackend.expect("GET", "/url", null, checkHeaders).respond(null);
|
|
3754
|
+
// $http.get("/url", { cache: testCache });
|
|
3755
|
+
|
|
3756
|
+
// $httpBackend.flush();
|
|
3757
|
+
// }));
|
|
3758
|
+
|
|
3759
|
+
// it("should not set an XSRF header for cross-domain requests", () => {
|
|
3760
|
+
// function checkHeaders(headers) {
|
|
3761
|
+
// return isUndefined(headers["X-XSRF-TOKEN"]);
|
|
3762
|
+
// }
|
|
3763
|
+
// const requestUrls = [
|
|
3764
|
+
// "https://api.example.com/path",
|
|
3765
|
+
// "http://trusted.example.com",
|
|
3766
|
+
// "https://trusted2.example.com:1338",
|
|
3767
|
+
// ];
|
|
3768
|
+
|
|
3769
|
+
// mockedCookies["XSRF-TOKEN"] = "secret";
|
|
3770
|
+
|
|
3771
|
+
// requestUrls.forEach((url) => {
|
|
3772
|
+
// $httpBackend.expect("GET", url, null, checkHeaders).respond(null);
|
|
3773
|
+
// $http.get(url);
|
|
3774
|
+
// $httpBackend.flush();
|
|
3775
|
+
// });
|
|
3776
|
+
// });
|
|
3777
|
+
|
|
3778
|
+
// it("should set an XSRF header for cross-domain requests to trusted origins", inject((
|
|
3779
|
+
// $browser,
|
|
3780
|
+
// ) => {
|
|
3781
|
+
// function checkHeaders(headers) {
|
|
3782
|
+
// return headers["X-XSRF-TOKEN"] === "secret";
|
|
3783
|
+
// }
|
|
3784
|
+
// const currentUrl = "https://example.com/path";
|
|
3785
|
+
// const requestUrls = [
|
|
3786
|
+
// "https://trusted.example.com/path",
|
|
3787
|
+
// "https://trusted2.example.com:1337/path",
|
|
3788
|
+
// ];
|
|
3789
|
+
|
|
3790
|
+
// $browser.url(currentUrl);
|
|
3791
|
+
// mockedCookies["XSRF-TOKEN"] = "secret";
|
|
3792
|
+
|
|
3793
|
+
// requestUrls.forEach((url) => {
|
|
3794
|
+
// $httpBackend.expect("GET", url, null, checkHeaders).respond(null);
|
|
3795
|
+
// $http.get(url);
|
|
3796
|
+
// $httpBackend.flush();
|
|
3797
|
+
// });
|
|
3798
|
+
// }));
|
|
3799
|
+
// });
|
|
3800
|
+
|
|
3801
|
+
// it("should pass timeout, withCredentials and responseType", () => {
|
|
3802
|
+
// const $httpBackend = jasmine.createSpy("$httpBackend");
|
|
3803
|
+
|
|
3804
|
+
// $httpBackend.and.callFake(
|
|
3805
|
+
// (m, u, d, c, h, timeout, withCredentials, responseType) => {
|
|
3806
|
+
// expect(timeout).toBe(12345);
|
|
3807
|
+
// expect(withCredentials).toBe(true);
|
|
3808
|
+
// expect(responseType).toBe("json");
|
|
3809
|
+
// },
|
|
3810
|
+
// );
|
|
3811
|
+
|
|
3812
|
+
// module(($provide) => {
|
|
3813
|
+
// $provide.value("$httpBackend", $httpBackend);
|
|
3814
|
+
// });
|
|
3815
|
+
|
|
3816
|
+
// inject(($http, $rootScope) => {
|
|
3817
|
+
// await $http({
|
|
3818
|
+
// method: "GET",
|
|
3819
|
+
// url: "some.html",
|
|
3820
|
+
// timeout: 12345,
|
|
3821
|
+
// withCredentials: true,
|
|
3822
|
+
// responseType: "json",
|
|
3823
|
+
// });
|
|
3824
|
+
// $rootScope.$digest();
|
|
3825
|
+
// expect($httpBackend).toHaveBeenCalled();
|
|
3826
|
+
// });
|
|
3827
|
+
|
|
3828
|
+
// $httpBackend.verifyNoOutstandingExpectation = () => {};
|
|
3829
|
+
// });
|
|
3830
|
+
|
|
3831
|
+
// it("should use withCredentials from default", () => {
|
|
3832
|
+
// const $httpBackend = jasmine.createSpy("$httpBackend");
|
|
3833
|
+
|
|
3834
|
+
// $httpBackend.and.callFake(
|
|
3835
|
+
// (m, u, d, c, h, timeout, withCredentials, responseType) => {
|
|
3836
|
+
// expect(withCredentials).toBe(true);
|
|
3837
|
+
// },
|
|
3838
|
+
// );
|
|
3839
|
+
|
|
3840
|
+
// module(($provide) => {
|
|
3841
|
+
// $provide.value("$httpBackend", $httpBackend);
|
|
3842
|
+
// });
|
|
3843
|
+
|
|
3844
|
+
// inject(($http, $rootScope) => {
|
|
3845
|
+
// $http.defaults.withCredentials = true;
|
|
3846
|
+
// await $http({
|
|
3847
|
+
// method: "GET",
|
|
3848
|
+
// url: "some.html",
|
|
3849
|
+
// timeout: 12345,
|
|
3850
|
+
// responseType: "json",
|
|
3851
|
+
// });
|
|
3852
|
+
// $rootScope.$digest();
|
|
3853
|
+
// expect($httpBackend).toHaveBeenCalled();
|
|
3854
|
+
// });
|
|
3855
|
+
|
|
3856
|
+
// $httpBackend.verifyNoOutstandingExpectation = () => {};
|
|
3857
|
+
// });
|
|
3858
|
+
// });
|
|
3859
|
+
|
|
3860
|
+
// describe("$http with $applyAsync", () => {
|
|
3861
|
+
// let $http;
|
|
3862
|
+
// let $httpBackend;
|
|
3863
|
+
// let $rootScope;
|
|
3864
|
+
// let $browser;
|
|
3865
|
+
// let log;
|
|
3866
|
+
|
|
3867
|
+
// beforeEach(inject([
|
|
3868
|
+
// "$http",
|
|
3869
|
+
// "$httpBackend",
|
|
3870
|
+
// "$rootScope",
|
|
3871
|
+
// "$browser",
|
|
3872
|
+
// "log",
|
|
3873
|
+
// function (http, backend, scope, browser, logger) {
|
|
3874
|
+
// $http = http;
|
|
3875
|
+
// $httpBackend = backend;
|
|
3876
|
+
// $rootScope = scope;
|
|
3877
|
+
// $browser = browser;
|
|
3878
|
+
// spyOn($rootScope, "$apply").and.callThrough();
|
|
3879
|
+
// spyOn($rootScope, "$applyAsync").and.callThrough();
|
|
3880
|
+
// spyOn($rootScope, "$digest").and.callThrough();
|
|
3881
|
+
// spyOn($browser.defer, "cancel").and.callThrough();
|
|
3882
|
+
// log = logger;
|
|
3883
|
+
// },
|
|
3884
|
+
// ]));
|
|
3885
|
+
|
|
3886
|
+
// it("should schedule coalesced apply on response", () => {
|
|
3887
|
+
// const handler = jasmine.createSpy("handler");
|
|
3888
|
+
// $httpBackend
|
|
3889
|
+
// .expect("GET", "/template1.html")
|
|
3890
|
+
// .respond(200, "<h1>Header!</h1>", {});
|
|
3891
|
+
// $http.get("/template1.html").then(handler);
|
|
3892
|
+
// // Ensure requests are sent
|
|
3893
|
+
// $rootScope.$digest();
|
|
3894
|
+
|
|
3895
|
+
// $httpBackend.flush(null, null, false);
|
|
3896
|
+
// expect($rootScope.$applyAsync).toHaveBeenCalled();
|
|
3897
|
+
// expect(handler).not.toHaveBeenCalled();
|
|
3898
|
+
|
|
3899
|
+
// $browser.defer.flush();
|
|
3900
|
+
// expect(handler).toHaveBeenCalled();
|
|
3901
|
+
// });
|
|
3902
|
+
|
|
3903
|
+
// it("should combine multiple responses within short time frame into a single $apply", () => {
|
|
3904
|
+
// $httpBackend
|
|
3905
|
+
// .expect("GET", "/template1.html")
|
|
3906
|
+
// .respond(200, "<h1>Header!</h1>", {});
|
|
3907
|
+
// $httpBackend
|
|
3908
|
+
// .expect("GET", "/template2.html")
|
|
3909
|
+
// .respond(200, "<p>Body!</p>", {});
|
|
3910
|
+
|
|
3911
|
+
// $http.get("/template1.html").then(log.fn("response 1"));
|
|
3912
|
+
// $http.get("/template2.html").then(log.fn("response 2"));
|
|
3913
|
+
// // Ensure requests are sent
|
|
3914
|
+
// $rootScope.$digest();
|
|
3915
|
+
|
|
3916
|
+
// $httpBackend.flush(null, null, false);
|
|
3917
|
+
// expect(log).toEqual([]);
|
|
3918
|
+
|
|
3919
|
+
// $browser.defer.flush();
|
|
3920
|
+
// expect(log).toEqual(["response 1", "response 2"]);
|
|
3921
|
+
// });
|
|
3922
|
+
|
|
3923
|
+
// it("should handle pending responses immediately if a digest occurs on $rootScope", () => {
|
|
3924
|
+
// $httpBackend
|
|
3925
|
+
// .expect("GET", "/template1.html")
|
|
3926
|
+
// .respond(200, "<h1>Header!</h1>", {});
|
|
3927
|
+
// $httpBackend
|
|
3928
|
+
// .expect("GET", "/template2.html")
|
|
3929
|
+
// .respond(200, "<p>Body!</p>", {});
|
|
3930
|
+
// $httpBackend
|
|
3931
|
+
// .expect("GET", "/template3.html")
|
|
3932
|
+
// .respond(200, "<p>Body!</p>", {});
|
|
3933
|
+
|
|
3934
|
+
// $http.get("/template1.html").then(log.fn("response 1"));
|
|
3935
|
+
// $http.get("/template2.html").then(log.fn("response 2"));
|
|
3936
|
+
// $http.get("/template3.html").then(log.fn("response 3"));
|
|
3937
|
+
// // Ensure requests are sent
|
|
3938
|
+
// $rootScope.$digest();
|
|
3939
|
+
|
|
3940
|
+
// // Intermediate $digest occurs before 3rd response is received, assert that pending responses
|
|
3941
|
+
// /// are handled
|
|
3942
|
+
// $httpBackend.flush(2);
|
|
3943
|
+
// expect(log).toEqual(["response 1", "response 2"]);
|
|
3944
|
+
|
|
3945
|
+
// // Finally, third response is received, and a second coalesced $apply is started
|
|
3946
|
+
// $httpBackend.flush(null, null, false);
|
|
3947
|
+
// $browser.defer.flush();
|
|
3948
|
+
// expect(log).toEqual(["response 1", "response 2", "response 3"]);
|
|
3949
|
+
// });
|
|
3950
|
+
// });
|
|
3951
|
+
|
|
3952
|
+
// describe("$http param serializers", () => {
|
|
3953
|
+
// let defSer;
|
|
3954
|
+
// let jqrSer;
|
|
3955
|
+
// beforeEach(inject(($httpParamSerializer, $httpParamSerializerJQLike) => {
|
|
3956
|
+
// defSer = $httpParamSerializer;
|
|
3957
|
+
// jqrSer = $httpParamSerializerJQLike;
|
|
3958
|
+
// }));
|
|
3959
|
+
|
|
3960
|
+
// describe("common functionality", () => {
|
|
3961
|
+
// it("should return empty string for null or undefined params", () => {
|
|
3962
|
+
// expect(defSer(undefined)).toEqual("");
|
|
3963
|
+
// expect(jqrSer(undefined)).toEqual("");
|
|
3964
|
+
// expect(defSer(null)).toEqual("");
|
|
3965
|
+
// expect(jqrSer(null)).toEqual("");
|
|
3966
|
+
// });
|
|
3967
|
+
|
|
3968
|
+
// it("should serialize objects", () => {
|
|
3969
|
+
// expect(defSer({ foo: "foov", bar: "barv" })).toEqual("bar=barv&foo=foov");
|
|
3970
|
+
// expect(jqrSer({ foo: "foov", bar: "barv" })).toEqual("bar=barv&foo=foov");
|
|
3971
|
+
// expect(
|
|
3972
|
+
// defSer({ someDate: new Date("2014-07-15T17:30:00.000Z") }),
|
|
3973
|
+
// ).toEqual("someDate=2014-07-15T17:30:00.000Z");
|
|
3974
|
+
// expect(
|
|
3975
|
+
// jqrSer({ someDate: new Date("2014-07-15T17:30:00.000Z") }),
|
|
3976
|
+
// ).toEqual("someDate=2014-07-15T17:30:00.000Z");
|
|
3977
|
+
// });
|
|
3978
|
+
// });
|
|
3979
|
+
|
|
3980
|
+
// describe("default array serialization", () => {
|
|
3981
|
+
// it("should serialize arrays by repeating param name", () => {
|
|
3982
|
+
// expect(defSer({ a: "b", foo: ["bar", "baz"] })).toEqual(
|
|
3983
|
+
// "a=b&foo=bar&foo=baz",
|
|
3984
|
+
// );
|
|
3985
|
+
// });
|
|
3986
|
+
|
|
3987
|
+
// it("should NOT serialize functions", () => {
|
|
3988
|
+
// expect(defSer({ foo: "foov", bar() {} })).toEqual("foo=foov");
|
|
3989
|
+
// });
|
|
3990
|
+
// });
|
|
3991
|
+
|
|
3992
|
+
// describe("jquery array and objects serialization", () => {
|
|
3993
|
+
// it("should serialize arrays by repeating param name with [] suffix", () => {
|
|
3994
|
+
// expect(jqrSer({ a: "b", foo: ["bar", "baz"] })).toEqual(
|
|
3995
|
+
// "a=b&foo%5B%5D=bar&foo%5B%5D=baz",
|
|
3996
|
+
// );
|
|
3997
|
+
// expect(
|
|
3998
|
+
// decodeURIComponent(jqrSer({ a: "b", foo: ["bar", "baz"] })),
|
|
3999
|
+
// ).toEqual("a=b&foo[]=bar&foo[]=baz");
|
|
4000
|
+
// });
|
|
4001
|
+
|
|
4002
|
+
// it("should serialize arrays with functions", () => {
|
|
4003
|
+
// expect(jqrSer({ foo: [valueFn("bar")] })).toEqual("foo%5B%5D=bar"); // foo[]=bar
|
|
4004
|
+
// });
|
|
4005
|
+
|
|
4006
|
+
// it("should serialize arrays with functions inside objects", () => {
|
|
4007
|
+
// expect(jqrSer({ foo: { bar: [valueFn("baz")] } })).toEqual(
|
|
4008
|
+
// "foo%5Bbar%5D%5B%5D=baz",
|
|
4009
|
+
// ); // foo[bar][]=baz
|
|
4010
|
+
// });
|
|
4011
|
+
|
|
4012
|
+
// it("should serialize objects by repeating param name with [key] suffix", () => {
|
|
4013
|
+
// expect(jqrSer({ a: "b", foo: { bar: "barv", baz: "bazv" } })).toEqual(
|
|
4014
|
+
// "a=b&foo%5Bbar%5D=barv&foo%5Bbaz%5D=bazv",
|
|
4015
|
+
// );
|
|
4016
|
+
// // a=b&foo[bar]=barv&foo[baz]=bazv
|
|
4017
|
+
// });
|
|
4018
|
+
|
|
4019
|
+
// it("should serialize objects with function properties", () => {
|
|
4020
|
+
// expect(jqrSer({ a: valueFn("b") })).toEqual("a=b");
|
|
4021
|
+
// });
|
|
4022
|
+
|
|
4023
|
+
// it("should serialize objects with function properties returning an object", () => {
|
|
4024
|
+
// expect(jqrSer({ a: valueFn({ b: "c" }) })).toEqual(
|
|
4025
|
+
// "a=%7B%22b%22:%22c%22%7D",
|
|
4026
|
+
// ); // a={"b":"c"}
|
|
4027
|
+
// });
|
|
4028
|
+
|
|
4029
|
+
// it("should serialize nested objects by repeating param name with [key] suffix", () => {
|
|
4030
|
+
// expect(
|
|
4031
|
+
// jqrSer({ a: ["b", { c: "d" }], e: { f: "g", h: ["i", "j"] } }),
|
|
4032
|
+
// ).toEqual(
|
|
4033
|
+
// "a%5B%5D=b&a%5B1%5D%5Bc%5D=d&e%5Bf%5D=g&e%5Bh%5D%5B%5D=i&e%5Bh%5D%5B%5D=j",
|
|
4034
|
+
// );
|
|
4035
|
+
// // a[]=b&a[1][c]=d&e[f]=g&e[h][]=i&e[h][]=j
|
|
4036
|
+
// });
|
|
4037
|
+
|
|
4038
|
+
// it("should serialize nested objects with function properties", () => {
|
|
4039
|
+
// expect(jqrSer({ foo: { bar: valueFn("barv") } })).toEqual(
|
|
4040
|
+
// "foo%5Bbar%5D=barv",
|
|
4041
|
+
// ); // foo[bar]=barv
|
|
4042
|
+
// });
|
|
4043
|
+
|
|
4044
|
+
// it("should serialize nested objects with function properties returning an object", () => {
|
|
4045
|
+
// expect(jqrSer({ foo: { bar: valueFn({ bav: "barv" }) } })).toEqual(
|
|
4046
|
+
// "foo%5Bbar%5D=%7B%22bav%22:%22barv%22%7D",
|
|
4047
|
+
// ); // foo[bar]={"bav":"barv"}
|
|
4048
|
+
// });
|
|
4049
|
+
|
|
4050
|
+
// it("should serialize objects inside array elements using their index", () => {
|
|
4051
|
+
// expect(
|
|
4052
|
+
// jqrSer({ a: ["b", "c"], d: [{ e: "f", g: "h" }, "i", { j: "k" }] }),
|
|
4053
|
+
// ).toEqual(
|
|
4054
|
+
// "a%5B%5D=b&a%5B%5D=c&d%5B0%5D%5Be%5D=f&d%5B0%5D%5Bg%5D=h&d%5B%5D=i&d%5B2%5D%5Bj%5D=k",
|
|
4055
|
+
// );
|
|
4056
|
+
// // a[]=b&a[]=c&d[0][e]=f&d[0][g]=h&d[]=i&d[2][j]=k
|
|
4057
|
+
// });
|
|
4058
|
+
// it("should serialize `null` and `undefined` elements as empty", () => {
|
|
4059
|
+
// expect(
|
|
4060
|
+
// jqrSer({
|
|
4061
|
+
// items: ["foo", "bar", null, undefined, "baz"],
|
|
4062
|
+
// x: null,
|
|
4063
|
+
// y: undefined,
|
|
4064
|
+
// }),
|
|
4065
|
+
// ).toEqual(
|
|
4066
|
+
// "items%5B%5D=foo&items%5B%5D=bar&items%5B%5D=&items%5B%5D=&items%5B%5D=baz&x=&y=",
|
|
4067
|
+
// );
|
|
4068
|
+
// // items[]=foo&items[]=bar&items[]=&items[]=&items[]=baz&x=&y=
|
|
4069
|
+
// });
|
|
4070
|
+
// });
|
|
4071
|
+
// });
|