Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit ce6e8e7

Browse files
committed
chore(http spec): Clean up spec file, use DI
1 parent d704313 commit ce6e8e7

File tree

1 file changed

+134
-137
lines changed

1 file changed

+134
-137
lines changed

test/http_spec.dart

+134-137
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import "_http.dart";
44
var VALUE = 'val';
55
var CACHED_VALUE = 'cached_value';
66

7-
87
class FakeCache implements Cache {
98
get(x) => x == 'f' ? new HttpResponse(200, CACHED_VALUE) : null;
109
put(_,__) => null;
@@ -16,160 +15,158 @@ class SubstringRewriter extends UrlRewriter {
1615
}
1716

1817
main() {
19-
describe('http rewriting', () {
20-
var rewriter, futures, backend, cache;
21-
beforeEach(() {
22-
rewriter = new SubstringRewriter();
18+
describe('http', () {
19+
var backend, cache;
20+
beforeEach(module((AngularModule module) {
2321
backend = new MockHttpBackend();
2422
cache = new FakeCache();
25-
});
26-
27-
it('should rewrite URLs before calling the backend', () {
28-
backend.expectGET('a', VALUE, times: 1);
29-
30-
var http = new Http(rewriter, backend);
31-
var called = 0;
32-
http.getString('a[not sent to backed]').then((v) {
33-
expect(v).toBe(VALUE);
34-
called += 1;
35-
});
36-
37-
expect(called).toEqual(0);
23+
module
24+
..value(HttpBackend, backend);
25+
}));
3826

39-
backend.flush();
27+
describe('url rewriting', () {
28+
beforeEach(module((AngularModule module) {
29+
module
30+
..type(UrlRewriter, SubstringRewriter);
31+
}));
4032

41-
expect(called).toEqual(1);
42-
backend.assertAllGetsCalled();
43-
});
4433

45-
it('should support pending requests for different raw URLs', () {
46-
backend.expectGET('a', VALUE, times: 1);
47-
48-
var http = new Http(rewriter, backend);
49-
var called = 0;
50-
http.getString('a[some string]', cache: cache).then((v) {
51-
expect(v).toBe(VALUE);
52-
called += 1;
53-
});
54-
http.getString('a[different string]', cache: cache).then((v) {
55-
expect(v).toBe(VALUE);
56-
called += 10;
57-
});
58-
59-
expect(called).toEqual(0);
60-
backend.flush();
61-
expect(called).toEqual(11);
62-
backend.assertAllGetsCalled();
63-
});
34+
it('should rewrite URLs before calling the backend', inject((Http http) {
35+
backend.expectGET('a', VALUE, times: 1);
6436

65-
it('should support caching', async(() {
66-
var http = new Http(rewriter, backend);
67-
var called = 0;
68-
http.getString('fromCache', cache: cache).then((v) {
69-
expect(v).toBe(CACHED_VALUE);
70-
called += 1;
71-
});
37+
var called = 0;
38+
http.getString('a[not sent to backed]').then((v) {
39+
expect(v).toBe(VALUE);
40+
called += 1;
41+
});
7242

73-
expect(called).toEqual(0);
74-
backend.flush();
75-
nextTurn();
43+
expect(called).toEqual(0);
7644

77-
expect(called).toEqual(1);
78-
backend.assertAllGetsCalled();
79-
}));
80-
});
45+
backend.flush();
8146

82-
describe('http caching', () {
83-
var rewriter, backend, cache;
84-
beforeEach(() {
85-
rewriter = new UrlRewriter();
86-
backend = new MockHttpBackend();
87-
cache = new FakeCache();
88-
});
89-
it('should not cache if no cache is present', () {
90-
backend.expectGET('a', VALUE, times: 2);
91-
92-
var http = new Http(rewriter, backend);
93-
var called = 0;
94-
http.getString('a').then((v) {
95-
expect(v).toBe(VALUE);
96-
called += 1;
97-
});
98-
http.getString('a').then((v) {
99-
expect(v).toBe(VALUE);
100-
called += 10;
101-
});
102-
103-
expect(called).toEqual(0);
104-
105-
backend.flush();
106-
107-
expect(called).toEqual(11);
108-
backend.assertAllGetsCalled();
109-
});
47+
expect(called).toEqual(1);
48+
backend.assertAllGetsCalled();
49+
}));
11050

11151

112-
it('should return a pending request', inject(() {
113-
backend.expectGET('a', VALUE, times: 1);
114-
115-
var http = new Http(rewriter, backend);
116-
var called = 0;
117-
http.getString('a', cache: cache).then((v) {
118-
expect(v).toBe(VALUE);
119-
called += 1;
120-
});
121-
http.getString('a', cache: cache).then((v) {
122-
expect(v).toBe(VALUE);
123-
called += 10;
124-
});
125-
126-
expect(called).toEqual(0);
127-
backend.flush();
128-
expect(called).toEqual(11);
129-
backend.assertAllGetsCalled();
130-
}));
52+
it('should support pending requests for different raw URLs', inject((Http http) {
53+
backend.expectGET('a', VALUE, times: 1);
13154

55+
var called = 0;
56+
http.getString('a[some string]', cache: cache).then((v) {
57+
expect(v).toBe(VALUE);
58+
called += 1;
59+
});
60+
http.getString('a[different string]', cache: cache).then((v) {
61+
expect(v).toBe(VALUE);
62+
called += 10;
63+
});
13264

133-
it('should not return a pending request after the request is complete', () {
134-
backend.expectGET('a', VALUE, times: 2);
65+
expect(called).toEqual(0);
66+
backend.flush();
67+
expect(called).toEqual(11);
68+
backend.assertAllGetsCalled();
69+
}));
13570

136-
var http = new Http(rewriter, backend);
137-
var called = 0;
138-
http.getString('a', cache: cache).then((v) {
139-
expect(v).toBe(VALUE);
140-
called += 1;
141-
});
14271

143-
expect(called).toEqual(0);
144-
backend.flush();
72+
it('should support caching', async(inject((Http http) {
73+
var called = 0;
74+
http.getString('fromCache', cache: cache).then((v) {
75+
expect(v).toBe(CACHED_VALUE);
76+
called += 1;
77+
});
14578

146-
http.getString('a', cache: cache).then((v) {
147-
expect(v).toBe(VALUE);
148-
called += 10;
149-
});
79+
expect(called).toEqual(0);
80+
backend.flush();
81+
nextTurn();
15082

151-
expect(called).toEqual(1);
152-
backend.flush();
153-
expect(called).toEqual(11);
154-
backend.assertAllGetsCalled();
83+
expect(called).toEqual(1);
84+
backend.assertAllGetsCalled();
85+
})));
15586
});
15687

157-
158-
it('should return a cached value if present', async(() {
159-
var http = new Http(rewriter, backend);
160-
var called = 0;
161-
// The URL string 'f' is primed in the FakeCache
162-
http.getString('f', cache: cache).then((v) {
163-
expect(v).toBe(CACHED_VALUE);
164-
called += 1;
165-
});
166-
167-
expect(called).toEqual(0);
168-
backend.flush();
169-
nextTurn();
170-
171-
expect(called).toEqual(1);
172-
backend.assertAllGetsCalled();
173-
}));
88+
describe('caching', () {
89+
it('should not cache if no cache is present', inject((Http http) {
90+
backend.expectGET('a', VALUE, times: 2);
91+
92+
var called = 0;
93+
http.getString('a').then((v) {
94+
expect(v).toBe(VALUE);
95+
called += 1;
96+
});
97+
http.getString('a').then((v) {
98+
expect(v).toBe(VALUE);
99+
called += 10;
100+
});
101+
102+
expect(called).toEqual(0);
103+
104+
backend.flush();
105+
106+
expect(called).toEqual(11);
107+
backend.assertAllGetsCalled();
108+
}));
109+
110+
111+
it('should return a pending request', inject((Http http) {
112+
backend.expectGET('a', VALUE, times: 1);
113+
114+
var called = 0;
115+
http.getString('a', cache: cache).then((v) {
116+
expect(v).toBe(VALUE);
117+
called += 1;
118+
});
119+
http.getString('a', cache: cache).then((v) {
120+
expect(v).toBe(VALUE);
121+
called += 10;
122+
});
123+
124+
expect(called).toEqual(0);
125+
backend.flush();
126+
expect(called).toEqual(11);
127+
backend.assertAllGetsCalled();
128+
}));
129+
130+
131+
it('should not return a pending request after the request is complete', inject((Http http) {
132+
backend.expectGET('a', VALUE, times: 2);
133+
134+
var called = 0;
135+
http.getString('a', cache: cache).then((v) {
136+
expect(v).toBe(VALUE);
137+
called += 1;
138+
});
139+
140+
expect(called).toEqual(0);
141+
backend.flush();
142+
143+
http.getString('a', cache: cache).then((v) {
144+
expect(v).toBe(VALUE);
145+
called += 10;
146+
});
147+
148+
expect(called).toEqual(1);
149+
backend.flush();
150+
expect(called).toEqual(11);
151+
backend.assertAllGetsCalled();
152+
}));
153+
154+
155+
it('should return a cached value if present', async(inject((Http http) {
156+
var called = 0;
157+
// The URL string 'f' is primed in the FakeCache
158+
http.getString('f', cache: cache).then((v) {
159+
expect(v).toBe(CACHED_VALUE);
160+
called += 1;
161+
});
162+
163+
expect(called).toEqual(0);
164+
backend.flush();
165+
nextTurn();
166+
167+
expect(called).toEqual(1);
168+
backend.assertAllGetsCalled();
169+
})));
170+
});
174171
});
175172
}

0 commit comments

Comments
 (0)