• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.volley;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.verifyNoMoreInteractions;
25 import static org.mockito.MockitoAnnotations.initMocks;
26 
27 import com.android.volley.Request.Method;
28 import com.android.volley.Request.Priority;
29 import com.android.volley.toolbox.NoCache;
30 import java.util.Collections;
31 import java.util.Map;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.robolectric.RobolectricTestRunner;
37 
38 @RunWith(RobolectricTestRunner.class)
39 public class RequestTest {
40     private @Mock ResponseDelivery mDelivery;
41     private @Mock Network mNetwork;
42 
43     @Before
setUp()44     public void setUp() throws Exception {
45         initMocks(this);
46     }
47 
48     @Test
compareTo()49     public void compareTo() {
50         int sequence = 0;
51         TestRequest low = new TestRequest(Priority.LOW);
52         low.setSequence(sequence++);
53         TestRequest low2 = new TestRequest(Priority.LOW);
54         low2.setSequence(sequence++);
55         TestRequest high = new TestRequest(Priority.HIGH);
56         high.setSequence(sequence++);
57         TestRequest immediate = new TestRequest(Priority.IMMEDIATE);
58         immediate.setSequence(sequence++);
59 
60         // "Low" should sort higher because it's really processing order.
61         assertTrue(low.compareTo(high) > 0);
62         assertTrue(high.compareTo(low) < 0);
63         assertTrue(low.compareTo(low2) < 0);
64         assertTrue(low.compareTo(immediate) > 0);
65         assertTrue(immediate.compareTo(high) < 0);
66     }
67 
68     private static class TestRequest extends Request<Object> {
69         private Priority mPriority = Priority.NORMAL;
70 
71         public TestRequest(Priority priority) {
72             super(Request.Method.GET, "", null);
73             mPriority = priority;
74         }
75 
76         @Override
77         public Priority getPriority() {
78             return mPriority;
79         }
80 
81         @Override
82         protected void deliverResponse(Object response) {}
83 
84         @Override
85         protected Response<Object> parseNetworkResponse(NetworkResponse response) {
86             return null;
87         }
88     }
89 
90     @Test
91     public void urlParsing() {
92         UrlParseRequest nullUrl = new UrlParseRequest(null);
93         assertEquals(0, nullUrl.getTrafficStatsTag());
94         UrlParseRequest emptyUrl = new UrlParseRequest("");
95         assertEquals(0, emptyUrl.getTrafficStatsTag());
96         UrlParseRequest noHost = new UrlParseRequest("http:///");
97         assertEquals(0, noHost.getTrafficStatsTag());
98         UrlParseRequest badProtocol = new UrlParseRequest("bad:http://foo");
99         assertEquals(0, badProtocol.getTrafficStatsTag());
100         UrlParseRequest goodProtocol = new UrlParseRequest("http://foo");
101         assertFalse(0 == goodProtocol.getTrafficStatsTag());
102     }
103 
104     @Test
105     public void getCacheKey() {
106         assertEquals(
107                 "http://example.com",
108                 new UrlParseRequest(Method.GET, "http://example.com").getCacheKey());
109         assertEquals(
110                 "http://example.com",
111                 new UrlParseRequest(Method.DEPRECATED_GET_OR_POST, "http://example.com")
112                         .getCacheKey());
113         assertEquals(
114                 "1-http://example.com",
115                 new UrlParseRequest(Method.POST, "http://example.com").getCacheKey());
116         assertEquals(
117                 "2-http://example.com",
118                 new UrlParseRequest(Method.PUT, "http://example.com").getCacheKey());
119     }
120 
121     private static class UrlParseRequest extends Request<Object> {
122         UrlParseRequest(String url) {
123             this(Method.GET, url);
124         }
125 
126         UrlParseRequest(int method, String url) {
127             super(method, url, null);
128         }
129 
130         @Override
131         protected void deliverResponse(Object response) {}
132 
133         @Override
134         protected Response<Object> parseNetworkResponse(NetworkResponse response) {
135             return null;
136         }
137     }
138 
139     @Test
140     public void nullKeyInPostParams() throws Exception {
141         Request<Object> request =
142                 new Request<Object>(Method.POST, "url", null) {
143                     @Override
144                     protected void deliverResponse(Object response) {}
145 
146                     @Override
147                     protected Response<Object> parseNetworkResponse(NetworkResponse response) {
148                         return null;
149                     }
150 
151                     @Override
152                     protected Map<String, String> getParams() {
153                         return Collections.singletonMap(null, "value");
154                     }
155 
156                     @Override
157                     protected Map<String, String> getPostParams() {
158                         return Collections.singletonMap(null, "value");
159                     }
160                 };
161         try {
162             request.getBody();
163         } catch (IllegalArgumentException e) {
164             // expected
165         }
166         try {
167             request.getPostBody();
168         } catch (IllegalArgumentException e) {
169             // expected
170         }
171     }
172 
173     @Test
174     public void nullValueInPostParams() throws Exception {
175         Request<Object> request =
176                 new Request<Object>(Method.POST, "url", null) {
177                     @Override
178                     protected void deliverResponse(Object response) {}
179 
180                     @Override
181                     protected Response<Object> parseNetworkResponse(NetworkResponse response) {
182                         return null;
183                     }
184 
185                     @Override
186                     protected Map<String, String> getParams() {
187                         return Collections.singletonMap("key", null);
188                     }
189 
190                     @Override
191                     protected Map<String, String> getPostParams() {
192                         return Collections.singletonMap("key", null);
193                     }
194                 };
195         try {
196             request.getBody();
197         } catch (IllegalArgumentException e) {
198             // expected
199         }
200         try {
201             request.getPostBody();
202         } catch (IllegalArgumentException e) {
203             // expected
204         }
205     }
206 
207     @Test
208     public void sendEvent_notifiesListeners() throws Exception {
209         RequestQueue.RequestEventListener listener = mock(RequestQueue.RequestEventListener.class);
210         RequestQueue queue = new RequestQueue(new NoCache(), mNetwork, 0, mDelivery);
211         queue.addRequestEventListener(listener);
212 
213         Request<Object> request =
214                 new Request<Object>(Method.POST, "url", null) {
215                     @Override
216                     protected void deliverResponse(Object response) {}
217 
218                     @Override
219                     protected Response<Object> parseNetworkResponse(NetworkResponse response) {
220                         return null;
221                     }
222                 };
223         request.setRequestQueue(queue);
224 
225         request.sendEvent(RequestQueue.RequestEvent.REQUEST_NETWORK_DISPATCH_STARTED);
226 
227         verify(listener)
228                 .onRequestEvent(
229                         request, RequestQueue.RequestEvent.REQUEST_NETWORK_DISPATCH_STARTED);
230         verifyNoMoreInteractions(listener);
231     }
232 }
233