| 21478 |
rajender |
1 |
/*
|
|
|
2 |
* Copyright (C) 2012 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.toolbox;
|
|
|
18 |
|
|
|
19 |
import com.android.volley.Request.Method;
|
|
|
20 |
import com.android.volley.mock.MockHttpURLConnection;
|
|
|
21 |
import com.android.volley.mock.TestRequest;
|
|
|
22 |
|
|
|
23 |
import org.junit.Before;
|
|
|
24 |
import org.junit.Test;
|
|
|
25 |
import org.junit.runner.RunWith;
|
|
|
26 |
import org.robolectric.RobolectricTestRunner;
|
|
|
27 |
|
|
|
28 |
import static org.junit.Assert.assertEquals;
|
|
|
29 |
import static org.junit.Assert.assertFalse;
|
|
|
30 |
import static org.junit.Assert.assertTrue;
|
|
|
31 |
|
|
|
32 |
@RunWith(RobolectricTestRunner.class)
|
|
|
33 |
public class HurlStackTest {
|
|
|
34 |
|
|
|
35 |
private MockHttpURLConnection mMockConnection;
|
|
|
36 |
|
|
|
37 |
@Before public void setUp() throws Exception {
|
|
|
38 |
mMockConnection = new MockHttpURLConnection();
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
@Test public void connectionForDeprecatedGetRequest() throws Exception {
|
|
|
42 |
TestRequest.DeprecatedGet request = new TestRequest.DeprecatedGet();
|
|
|
43 |
assertEquals(request.getMethod(), Method.DEPRECATED_GET_OR_POST);
|
|
|
44 |
|
|
|
45 |
HurlStack.setConnectionParametersForRequest(mMockConnection, request);
|
|
|
46 |
assertEquals("GET", mMockConnection.getRequestMethod());
|
|
|
47 |
assertFalse(mMockConnection.getDoOutput());
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
@Test public void connectionForDeprecatedPostRequest() throws Exception {
|
|
|
51 |
TestRequest.DeprecatedPost request = new TestRequest.DeprecatedPost();
|
|
|
52 |
assertEquals(request.getMethod(), Method.DEPRECATED_GET_OR_POST);
|
|
|
53 |
|
|
|
54 |
HurlStack.setConnectionParametersForRequest(mMockConnection, request);
|
|
|
55 |
assertEquals("POST", mMockConnection.getRequestMethod());
|
|
|
56 |
assertTrue(mMockConnection.getDoOutput());
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
@Test public void connectionForGetRequest() throws Exception {
|
|
|
60 |
TestRequest.Get request = new TestRequest.Get();
|
|
|
61 |
assertEquals(request.getMethod(), Method.GET);
|
|
|
62 |
|
|
|
63 |
HurlStack.setConnectionParametersForRequest(mMockConnection, request);
|
|
|
64 |
assertEquals("GET", mMockConnection.getRequestMethod());
|
|
|
65 |
assertFalse(mMockConnection.getDoOutput());
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
@Test public void connectionForPostRequest() throws Exception {
|
|
|
69 |
TestRequest.Post request = new TestRequest.Post();
|
|
|
70 |
assertEquals(request.getMethod(), Method.POST);
|
|
|
71 |
|
|
|
72 |
HurlStack.setConnectionParametersForRequest(mMockConnection, request);
|
|
|
73 |
assertEquals("POST", mMockConnection.getRequestMethod());
|
|
|
74 |
assertFalse(mMockConnection.getDoOutput());
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
@Test public void connectionForPostWithBodyRequest() throws Exception {
|
|
|
78 |
TestRequest.PostWithBody request = new TestRequest.PostWithBody();
|
|
|
79 |
assertEquals(request.getMethod(), Method.POST);
|
|
|
80 |
|
|
|
81 |
HurlStack.setConnectionParametersForRequest(mMockConnection, request);
|
|
|
82 |
assertEquals("POST", mMockConnection.getRequestMethod());
|
|
|
83 |
assertTrue(mMockConnection.getDoOutput());
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
@Test public void connectionForPutRequest() throws Exception {
|
|
|
87 |
TestRequest.Put request = new TestRequest.Put();
|
|
|
88 |
assertEquals(request.getMethod(), Method.PUT);
|
|
|
89 |
|
|
|
90 |
HurlStack.setConnectionParametersForRequest(mMockConnection, request);
|
|
|
91 |
assertEquals("PUT", mMockConnection.getRequestMethod());
|
|
|
92 |
assertFalse(mMockConnection.getDoOutput());
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
@Test public void connectionForPutWithBodyRequest() throws Exception {
|
|
|
96 |
TestRequest.PutWithBody request = new TestRequest.PutWithBody();
|
|
|
97 |
assertEquals(request.getMethod(), Method.PUT);
|
|
|
98 |
|
|
|
99 |
HurlStack.setConnectionParametersForRequest(mMockConnection, request);
|
|
|
100 |
assertEquals("PUT", mMockConnection.getRequestMethod());
|
|
|
101 |
assertTrue(mMockConnection.getDoOutput());
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
@Test public void connectionForDeleteRequest() throws Exception {
|
|
|
105 |
TestRequest.Delete request = new TestRequest.Delete();
|
|
|
106 |
assertEquals(request.getMethod(), Method.DELETE);
|
|
|
107 |
|
|
|
108 |
HurlStack.setConnectionParametersForRequest(mMockConnection, request);
|
|
|
109 |
assertEquals("DELETE", mMockConnection.getRequestMethod());
|
|
|
110 |
assertFalse(mMockConnection.getDoOutput());
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
@Test public void connectionForHeadRequest() throws Exception {
|
|
|
114 |
TestRequest.Head request = new TestRequest.Head();
|
|
|
115 |
assertEquals(request.getMethod(), Method.HEAD);
|
|
|
116 |
|
|
|
117 |
HurlStack.setConnectionParametersForRequest(mMockConnection, request);
|
|
|
118 |
assertEquals("HEAD", mMockConnection.getRequestMethod());
|
|
|
119 |
assertFalse(mMockConnection.getDoOutput());
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
@Test public void connectionForOptionsRequest() throws Exception {
|
|
|
123 |
TestRequest.Options request = new TestRequest.Options();
|
|
|
124 |
assertEquals(request.getMethod(), Method.OPTIONS);
|
|
|
125 |
|
|
|
126 |
HurlStack.setConnectionParametersForRequest(mMockConnection, request);
|
|
|
127 |
assertEquals("OPTIONS", mMockConnection.getRequestMethod());
|
|
|
128 |
assertFalse(mMockConnection.getDoOutput());
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
@Test public void connectionForTraceRequest() throws Exception {
|
|
|
132 |
TestRequest.Trace request = new TestRequest.Trace();
|
|
|
133 |
assertEquals(request.getMethod(), Method.TRACE);
|
|
|
134 |
|
|
|
135 |
HurlStack.setConnectionParametersForRequest(mMockConnection, request);
|
|
|
136 |
assertEquals("TRACE", mMockConnection.getRequestMethod());
|
|
|
137 |
assertFalse(mMockConnection.getDoOutput());
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
@Test public void connectionForPatchRequest() throws Exception {
|
|
|
141 |
TestRequest.Patch request = new TestRequest.Patch();
|
|
|
142 |
assertEquals(request.getMethod(), Method.PATCH);
|
|
|
143 |
|
|
|
144 |
HurlStack.setConnectionParametersForRequest(mMockConnection, request);
|
|
|
145 |
assertEquals("PATCH", mMockConnection.getRequestMethod());
|
|
|
146 |
assertFalse(mMockConnection.getDoOutput());
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
@Test public void connectionForPatchWithBodyRequest() throws Exception {
|
|
|
150 |
TestRequest.PatchWithBody request = new TestRequest.PatchWithBody();
|
|
|
151 |
assertEquals(request.getMethod(), Method.PATCH);
|
|
|
152 |
|
|
|
153 |
HurlStack.setConnectionParametersForRequest(mMockConnection, request);
|
|
|
154 |
assertEquals("PATCH", mMockConnection.getRequestMethod());
|
|
|
155 |
assertTrue(mMockConnection.getDoOutput());
|
|
|
156 |
}
|
|
|
157 |
}
|