|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This program is licensed to you under the Apache License Version 2.0, |
| 5 | + * and you may not use this file except in compliance with the Apache License Version 2.0. |
| 6 | + * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, |
| 9 | + * software distributed under the Apache License Version 2.0 is distributed on an |
| 10 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 12 | + */ |
| 13 | +package com.ning.http.client.async; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.io.IOException; |
| 17 | + |
| 18 | +import javax.servlet.ServletException; |
| 19 | +import javax.servlet.http.HttpServletRequest; |
| 20 | +import javax.servlet.http.HttpServletResponse; |
| 21 | + |
| 22 | +import org.eclipse.jetty.server.Request; |
| 23 | +import org.eclipse.jetty.server.handler.AbstractHandler; |
| 24 | +import org.testng.Assert; |
| 25 | +import org.testng.annotations.Test; |
| 26 | + |
| 27 | +import com.ning.http.client.AsyncHttpClient; |
| 28 | +import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; |
| 29 | +import com.ning.http.client.FilePart; |
| 30 | +import com.ning.http.client.Response; |
| 31 | + |
| 32 | +public abstract class FastUnauthorizedUploadTest extends AbstractBasicTest { |
| 33 | + |
| 34 | + @Override |
| 35 | + public AbstractHandler configureHandler() throws Exception { |
| 36 | + return new AbstractHandler() { |
| 37 | + |
| 38 | + public void handle(String target, Request baseRequest, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { |
| 39 | + |
| 40 | + resp.setStatus(401); |
| 41 | + resp.getOutputStream().flush(); |
| 42 | + resp.getOutputStream().close(); |
| 43 | + |
| 44 | + baseRequest.setHandled(true); |
| 45 | + } |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + @Test(groups = { "standalone", "default_provider" }, enabled = true) |
| 50 | + public void testUnauthorizedWhileUploading() throws Exception { |
| 51 | + byte[] bytes = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile".getBytes("UTF-16"); |
| 52 | + long repeats = (1024 * 1024 / bytes.length) + 1; |
| 53 | + File largeFile = FilePartLargeFileTest.createTempFile(bytes, (int) repeats); |
| 54 | + |
| 55 | + AsyncHttpClient client = getAsyncHttpClient(null); |
| 56 | + try { |
| 57 | + BoundRequestBuilder rb = client.preparePut(getTargetUrl()); |
| 58 | + |
| 59 | + rb.addBodyPart(new FilePart("test", largeFile, "application/octet-stream", "UTF-8")); |
| 60 | + |
| 61 | + Response response = rb.execute().get(); |
| 62 | + Assert.assertEquals(401, response.getStatusCode()); |
| 63 | + } finally { |
| 64 | + client.close(); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments