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.TestRequest;
21 import com.android.volley.toolbox.HttpClientStack.HttpPatch;
22 
23 import org.apache.http.client.methods.HttpDelete;
24 import org.apache.http.client.methods.HttpGet;
25 import org.apache.http.client.methods.HttpHead;
26 import org.apache.http.client.methods.HttpOptions;
27 import org.apache.http.client.methods.HttpPost;
28 import org.apache.http.client.methods.HttpPut;
29 import org.apache.http.client.methods.HttpTrace;
30 import org.apache.http.client.methods.HttpUriRequest;
31 
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.robolectric.RobolectricTestRunner;
35 
36 import static org.junit.Assert.*;
37 
38 @RunWith(RobolectricTestRunner.class)
39 public class HttpClientStackTest {
40 
createDeprecatedGetRequest()41     @Test public void createDeprecatedGetRequest() throws Exception {
42         TestRequest.DeprecatedGet request = new TestRequest.DeprecatedGet();
43         assertEquals(request.getMethod(), Method.DEPRECATED_GET_OR_POST);
44 
45         HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
46         assertTrue(httpRequest instanceof HttpGet);
47     }
48 
createDeprecatedPostRequest()49     @Test public void createDeprecatedPostRequest() throws Exception {
50         TestRequest.DeprecatedPost request = new TestRequest.DeprecatedPost();
51         assertEquals(request.getMethod(), Method.DEPRECATED_GET_OR_POST);
52 
53         HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
54         assertTrue(httpRequest instanceof HttpPost);
55     }
56 
createGetRequest()57     @Test public void createGetRequest() throws Exception {
58         TestRequest.Get request = new TestRequest.Get();
59         assertEquals(request.getMethod(), Method.GET);
60 
61         HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
62         assertTrue(httpRequest instanceof HttpGet);
63     }
64 
createPostRequest()65     @Test public void createPostRequest() throws Exception {
66         TestRequest.Post request = new TestRequest.Post();
67         assertEquals(request.getMethod(), Method.POST);
68 
69         HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
70         assertTrue(httpRequest instanceof HttpPost);
71     }
72 
createPostRequestWithBody()73     @Test public void createPostRequestWithBody() throws Exception {
74         TestRequest.PostWithBody request = new TestRequest.PostWithBody();
75         assertEquals(request.getMethod(), Method.POST);
76 
77         HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
78         assertTrue(httpRequest instanceof HttpPost);
79     }
80 
createPutRequest()81     @Test public void createPutRequest() throws Exception {
82         TestRequest.Put request = new TestRequest.Put();
83         assertEquals(request.getMethod(), Method.PUT);
84 
85         HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
86         assertTrue(httpRequest instanceof HttpPut);
87     }
88 
createPutRequestWithBody()89     @Test public void createPutRequestWithBody() throws Exception {
90         TestRequest.PutWithBody request = new TestRequest.PutWithBody();
91         assertEquals(request.getMethod(), Method.PUT);
92 
93         HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
94         assertTrue(httpRequest instanceof HttpPut);
95     }
96 
createDeleteRequest()97     @Test public void createDeleteRequest() throws Exception {
98         TestRequest.Delete request = new TestRequest.Delete();
99         assertEquals(request.getMethod(), Method.DELETE);
100 
101         HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
102         assertTrue(httpRequest instanceof HttpDelete);
103     }
104 
createHeadRequest()105     @Test public void createHeadRequest() throws Exception {
106         TestRequest.Head request = new TestRequest.Head();
107         assertEquals(request.getMethod(), Method.HEAD);
108 
109         HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
110         assertTrue(httpRequest instanceof HttpHead);
111     }
112 
createOptionsRequest()113     @Test public void createOptionsRequest() throws Exception {
114         TestRequest.Options request = new TestRequest.Options();
115         assertEquals(request.getMethod(), Method.OPTIONS);
116 
117         HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
118         assertTrue(httpRequest instanceof HttpOptions);
119     }
120 
createTraceRequest()121     @Test public void createTraceRequest() throws Exception {
122         TestRequest.Trace request = new TestRequest.Trace();
123         assertEquals(request.getMethod(), Method.TRACE);
124 
125         HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
126         assertTrue(httpRequest instanceof HttpTrace);
127     }
128 
createPatchRequest()129     @Test public void createPatchRequest() throws Exception {
130         TestRequest.Patch request = new TestRequest.Patch();
131         assertEquals(request.getMethod(), Method.PATCH);
132 
133         HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
134         assertTrue(httpRequest instanceof HttpPatch);
135     }
136 
createPatchRequestWithBody()137     @Test public void createPatchRequestWithBody() throws Exception {
138         TestRequest.PatchWithBody request = new TestRequest.PatchWithBody();
139         assertEquals(request.getMethod(), Method.PATCH);
140 
141         HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
142         assertTrue(httpRequest instanceof HttpPatch);
143     }
144 }
145