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 17package com.android.volley.toolbox; 18 19import com.android.volley.Request.Method; 20import com.android.volley.mock.MockHttpURLConnection; 21import com.android.volley.mock.TestRequest; 22 23import android.test.AndroidTestCase; 24import android.test.suitebuilder.annotation.SmallTest; 25 26@SmallTest 27public class HurlStackTest extends AndroidTestCase { 28 29 private MockHttpURLConnection mMockConnection; 30 31 @Override 32 protected void setUp() throws Exception { 33 super.setUp(); 34 mContext = getContext(); 35 mMockConnection = new MockHttpURLConnection(); 36 } 37 38 public void testConnectionForDeprecatedGetRequest() throws Exception { 39 TestRequest.DeprecatedGet request = new TestRequest.DeprecatedGet(); 40 assertEquals(request.getMethod(), Method.DEPRECATED_GET_OR_POST); 41 42 HurlStack.setConnectionParametersForRequest(mMockConnection, request); 43 assertEquals("GET", mMockConnection.getRequestMethod()); 44 assertFalse(mMockConnection.getDoOutput()); 45 } 46 47 public void testConnectionForDeprecatedPostRequest() throws Exception { 48 TestRequest.DeprecatedPost request = new TestRequest.DeprecatedPost(); 49 assertEquals(request.getMethod(), Method.DEPRECATED_GET_OR_POST); 50 51 HurlStack.setConnectionParametersForRequest(mMockConnection, request); 52 assertEquals("POST", mMockConnection.getRequestMethod()); 53 assertTrue(mMockConnection.getDoOutput()); 54 } 55 56 public void testConnectionForGetRequest() throws Exception { 57 TestRequest.Get request = new TestRequest.Get(); 58 assertEquals(request.getMethod(), Method.GET); 59 60 HurlStack.setConnectionParametersForRequest(mMockConnection, request); 61 assertEquals("GET", mMockConnection.getRequestMethod()); 62 assertFalse(mMockConnection.getDoOutput()); 63 } 64 65 public void testConnectionForPostRequest() throws Exception { 66 TestRequest.Post request = new TestRequest.Post(); 67 assertEquals(request.getMethod(), Method.POST); 68 69 HurlStack.setConnectionParametersForRequest(mMockConnection, request); 70 assertEquals("POST", mMockConnection.getRequestMethod()); 71 assertFalse(mMockConnection.getDoOutput()); 72 } 73 74 public void testConnectionForPostWithBodyRequest() throws Exception { 75 TestRequest.PostWithBody request = new TestRequest.PostWithBody(); 76 assertEquals(request.getMethod(), Method.POST); 77 78 HurlStack.setConnectionParametersForRequest(mMockConnection, request); 79 assertEquals("POST", mMockConnection.getRequestMethod()); 80 assertTrue(mMockConnection.getDoOutput()); 81 } 82 83 public void testConnectionForPutRequest() throws Exception { 84 TestRequest.Put request = new TestRequest.Put(); 85 assertEquals(request.getMethod(), Method.PUT); 86 87 HurlStack.setConnectionParametersForRequest(mMockConnection, request); 88 assertEquals("PUT", mMockConnection.getRequestMethod()); 89 assertFalse(mMockConnection.getDoOutput()); 90 } 91 92 public void testConnectionForPutWithBodyRequest() throws Exception { 93 TestRequest.PutWithBody request = new TestRequest.PutWithBody(); 94 assertEquals(request.getMethod(), Method.PUT); 95 96 HurlStack.setConnectionParametersForRequest(mMockConnection, request); 97 assertEquals("PUT", mMockConnection.getRequestMethod()); 98 assertTrue(mMockConnection.getDoOutput()); 99 } 100 101 public void testConnectionForDeleteRequest() throws Exception { 102 TestRequest.Delete request = new TestRequest.Delete(); 103 assertEquals(request.getMethod(), Method.DELETE); 104 105 HurlStack.setConnectionParametersForRequest(mMockConnection, request); 106 assertEquals("DELETE", mMockConnection.getRequestMethod()); 107 assertFalse(mMockConnection.getDoOutput()); 108 } 109} 110