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