@@ -4,7 +4,6 @@ import "_http.dart";
4
4
var VALUE = 'val' ;
5
5
var CACHED_VALUE = 'cached_value' ;
6
6
7
-
8
7
class FakeCache implements Cache {
9
8
get (x) => x == 'f' ? new HttpResponse (200 , CACHED_VALUE ) : null ;
10
9
put (_,__) => null ;
@@ -16,160 +15,158 @@ class SubstringRewriter extends UrlRewriter {
16
15
}
17
16
18
17
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) {
23
21
backend = new MockHttpBackend ();
24
22
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
+ }));
38
26
39
- backend.flush ();
27
+ describe ('url rewriting' , () {
28
+ beforeEach (module ((AngularModule module) {
29
+ module
30
+ ..type (UrlRewriter , SubstringRewriter );
31
+ }));
40
32
41
- expect (called).toEqual (1 );
42
- backend.assertAllGetsCalled ();
43
- });
44
33
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 );
64
36
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
+ });
72
42
73
- expect (called).toEqual (0 );
74
- backend.flush ();
75
- nextTurn ();
43
+ expect (called).toEqual (0 );
76
44
77
- expect (called).toEqual (1 );
78
- backend.assertAllGetsCalled ();
79
- }));
80
- });
45
+ backend.flush ();
81
46
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
+ }));
110
50
111
51
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 );
131
54
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
+ });
132
64
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
+ }));
135
70
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
- });
142
71
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
+ });
145
78
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 ();
150
82
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
+ })));
155
86
});
156
87
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
+ });
174
171
});
175
172
}
0 commit comments