1 /* 2 * Copyright (C) 2022 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 android.net.http.cts.util 18 19 import android.content.Context 20 import android.webkit.cts.CtsTestServer 21 import java.net.URI 22 import org.apache.http.HttpEntityEnclosingRequest 23 import org.apache.http.HttpRequest 24 import org.apache.http.HttpResponse 25 import org.apache.http.HttpStatus 26 import org.apache.http.HttpVersion 27 import org.apache.http.message.BasicHttpResponse 28 29 private const val ECHO_BODY_PATH = "/echo_body" 30 31 /** Extends CtsTestServer to handle POST requests and other test specific requests */ 32 class HttpCtsTestServer(context: Context) : CtsTestServer(context) { 33 34 val echoBodyUrl: String = baseUri + ECHO_BODY_PATH 35 val successUrl: String = getAssetUrl("html/hello_world.html") 36 onPostnull37 override fun onPost(req: HttpRequest): HttpResponse? { 38 val path = URI.create(req.requestLine.uri).path 39 var response: HttpResponse? = null 40 41 if (path.startsWith(ECHO_BODY_PATH)) { 42 if (req !is HttpEntityEnclosingRequest) { 43 return BasicHttpResponse( 44 HttpVersion.HTTP_1_0, 45 HttpStatus.SC_INTERNAL_SERVER_ERROR, 46 "Expected req to be of type HttpEntityEnclosingRequest but got ${req.javaClass}" 47 ) 48 } 49 50 response = BasicHttpResponse(HttpVersion.HTTP_1_0, HttpStatus.SC_OK, null) 51 response.entity = req.entity 52 response.addHeader("Content-Length", req.entity.contentLength.toString()) 53 } 54 55 return response 56 } 57 } 58