|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 AsyncHttpClient Project. 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 |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, |
| 10 | + * software distributed under the Apache License Version 2.0 is distributed on an |
| 11 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 13 | + */ |
| 14 | +package org.asynchttpclient.request.body.multipart; |
| 15 | + |
| 16 | +import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH; |
| 17 | +import static io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_OCTET_STREAM; |
| 18 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 19 | +import static org.asynchttpclient.Dsl.*; |
| 20 | +import static org.asynchttpclient.test.TestUtils.*; |
| 21 | +import static org.testng.Assert.assertEquals; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | + |
| 25 | +import org.asynchttpclient.AbstractBasicTest; |
| 26 | +import org.asynchttpclient.AsyncHttpClient; |
| 27 | +import org.asynchttpclient.BasicAuthTest; |
| 28 | +import org.asynchttpclient.Response; |
| 29 | +import org.eclipse.jetty.server.Server; |
| 30 | +import org.eclipse.jetty.server.ServerConnector; |
| 31 | +import org.eclipse.jetty.server.handler.AbstractHandler; |
| 32 | +import org.testng.annotations.BeforeClass; |
| 33 | +import org.testng.annotations.Test; |
| 34 | + |
| 35 | +public class MultipartBasicAuthTest extends AbstractBasicTest { |
| 36 | + |
| 37 | + @BeforeClass(alwaysRun = true) |
| 38 | + @Override |
| 39 | + public void setUpGlobal() throws Exception { |
| 40 | + |
| 41 | + server = new Server(); |
| 42 | + ServerConnector connector1 = addHttpConnector(server); |
| 43 | + addBasicAuthHandler(server, configureHandler()); |
| 44 | + server.start(); |
| 45 | + port1 = connector1.getLocalPort(); |
| 46 | + logger.info("Local HTTP server started successfully"); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public AbstractHandler configureHandler() throws Exception { |
| 51 | + return new BasicAuthTest.SimpleHandler(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test(groups = "standalone", enabled = false) |
| 55 | + public void testNoRealm() throws Exception { |
| 56 | + File file = createTempFile(1024 * 1024); |
| 57 | + |
| 58 | + try (AsyncHttpClient client = asyncHttpClient()) { |
| 59 | + for (int i = 0; i < 20; i++) { |
| 60 | + Response response = client.preparePut(getTargetUrl())// |
| 61 | + .addBodyPart(new FilePart("test", file, APPLICATION_OCTET_STREAM.toString(), UTF_8)).execute().get(); |
| 62 | + assertEquals(response.getStatusCode(), 401); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test(groups = "standalone", enabled = false) |
| 68 | + public void testAuthorizedRealm() throws Exception { |
| 69 | + File file = createTempFile(1024 * 1024); |
| 70 | + |
| 71 | + try (AsyncHttpClient client = asyncHttpClient()) { |
| 72 | + for (int i = 0; i < 20; i++) { |
| 73 | + Response response = client.preparePut(getTargetUrl())// |
| 74 | + .setRealm(basicAuthRealm(USER, ADMIN).build())// |
| 75 | + .addBodyPart(new FilePart("test", file, APPLICATION_OCTET_STREAM.toString(), UTF_8)).execute().get(); |
| 76 | + assertEquals(response.getStatusCode(), 200); |
| 77 | + assertEquals(response.getResponseBodyAsBytes().length, Integer.valueOf(response.getHeader("X-" + CONTENT_LENGTH)).intValue()); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments