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.mock; 18 19 import com.android.volley.NetworkResponse; 20 import com.android.volley.Request; 21 import com.android.volley.Response; 22 23 import java.util.HashMap; 24 import java.util.Map; 25 26 public class TestRequest { 27 private static final String TEST_URL = "http://foo.com"; 28 29 /** Base Request class for testing allowing both the deprecated and new constructor. */ 30 private static class Base extends Request<byte[]> { 31 @SuppressWarnings("deprecation") Base(String url, Response.ErrorListener listener)32 public Base(String url, Response.ErrorListener listener) { 33 super(url, listener); 34 } 35 Base(int method, String url, Response.ErrorListener listener)36 public Base(int method, String url, Response.ErrorListener listener) { 37 super(method, url, listener); 38 } 39 40 @Override parseNetworkResponse(NetworkResponse response)41 protected Response<byte[]> parseNetworkResponse(NetworkResponse response) { 42 return null; 43 } 44 45 @Override deliverResponse(byte[] response)46 protected void deliverResponse(byte[] response) { 47 } 48 } 49 50 /** Test example of a GET request in the deprecated style. */ 51 public static class DeprecatedGet extends Base { DeprecatedGet()52 public DeprecatedGet() { 53 super(TEST_URL, null); 54 } 55 } 56 57 /** Test example of a POST request in the deprecated style. */ 58 public static class DeprecatedPost extends Base { 59 private Map<String, String> mPostParams; 60 DeprecatedPost()61 public DeprecatedPost() { 62 super(TEST_URL, null); 63 mPostParams = new HashMap<String, String>(); 64 mPostParams.put("requestpost", "foo"); 65 } 66 67 @Override getPostParams()68 protected Map<String, String> getPostParams() { 69 return mPostParams; 70 } 71 } 72 73 /** Test example of a GET request in the new style. */ 74 public static class Get extends Base { Get()75 public Get() { 76 super(Method.GET, TEST_URL, null); 77 } 78 } 79 80 /** 81 * Test example of a POST request in the new style. In the new style, it is possible 82 * to have a POST with no body. 83 */ 84 public static class Post extends Base { Post()85 public Post() { 86 super(Method.POST, TEST_URL, null); 87 } 88 } 89 90 /** Test example of a POST request in the new style with a body. */ 91 public static class PostWithBody extends Post { 92 private Map<String, String> mParams; 93 PostWithBody()94 public PostWithBody() { 95 mParams = new HashMap<String, String>(); 96 mParams.put("testKey", "testValue"); 97 } 98 99 @Override getParams()100 public Map<String, String> getParams() { 101 return mParams; 102 } 103 } 104 105 /** 106 * Test example of a PUT request in the new style. In the new style, it is possible to have a 107 * PUT with no body. 108 */ 109 public static class Put extends Base { Put()110 public Put() { 111 super(Method.PUT, TEST_URL, null); 112 } 113 } 114 115 /** Test example of a PUT request in the new style with a body. */ 116 public static class PutWithBody extends Put { 117 private Map<String, String> mParams = new HashMap<String, String>(); 118 PutWithBody()119 public PutWithBody() { 120 mParams = new HashMap<String, String>(); 121 mParams.put("testKey", "testValue"); 122 } 123 124 @Override getParams()125 public Map<String, String> getParams() { 126 return mParams; 127 } 128 } 129 130 /** Test example of a DELETE request in the new style. */ 131 public static class Delete extends Base { Delete()132 public Delete() { 133 super(Method.DELETE, TEST_URL, null); 134 } 135 } 136 137 /** Test example of a HEAD request in the new style. */ 138 public static class Head extends Base { Head()139 public Head() { 140 super(Method.HEAD, TEST_URL, null); 141 } 142 } 143 144 /** Test example of a OPTIONS request in the new style. */ 145 public static class Options extends Base { Options()146 public Options() { 147 super(Method.OPTIONS, TEST_URL, null); 148 } 149 } 150 151 /** Test example of a TRACE request in the new style. */ 152 public static class Trace extends Base { Trace()153 public Trace() { 154 super(Method.TRACE, TEST_URL, null); 155 } 156 } 157 158 /** Test example of a PATCH request in the new style. */ 159 public static class Patch extends Base { Patch()160 public Patch() { 161 super(Method.PATCH, TEST_URL, null); 162 } 163 } 164 165 /** Test example of a PATCH request in the new style with a body. */ 166 public static class PatchWithBody extends Patch { 167 private Map<String, String> mParams = new HashMap<String, String>(); 168 PatchWithBody()169 public PatchWithBody() { 170 mParams = new HashMap<String, String>(); 171 mParams.put("testKey", "testValue"); 172 } 173 174 @Override getParams()175 public Map<String, String> getParams() { 176 return mParams; 177 } 178 } 179 } 180