|
| 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 | +packageorg.asynchttpclient.request.body.multipart; |
| 15 | + |
| 16 | +importstaticio.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH; |
| 17 | +importstaticio.netty.handler.codec.http.HttpHeaderValues.APPLICATION_OCTET_STREAM; |
| 18 | +importstaticjava.nio.charset.StandardCharsets.UTF_8; |
| 19 | +importstaticorg.asynchttpclient.Dsl.*; |
| 20 | +importstaticorg.asynchttpclient.test.TestUtils.*; |
| 21 | +importstaticorg.testng.Assert.assertEquals; |
| 22 | + |
| 23 | +importjava.io.File; |
| 24 | + |
| 25 | +importorg.asynchttpclient.AbstractBasicTest; |
| 26 | +importorg.asynchttpclient.AsyncHttpClient; |
| 27 | +importorg.asynchttpclient.BasicAuthTest; |
| 28 | +importorg.asynchttpclient.Response; |
| 29 | +importorg.eclipse.jetty.server.Server; |
| 30 | +importorg.eclipse.jetty.server.ServerConnector; |
| 31 | +importorg.eclipse.jetty.server.handler.AbstractHandler; |
| 32 | +importorg.testng.annotations.BeforeClass; |
| 33 | +importorg.testng.annotations.Test; |
| 34 | + |
| 35 | +publicclassMultipartBasicAuthTestextendsAbstractBasicTest { |
| 36 | + |
| 37 | +@BeforeClass(alwaysRun =true) |
| 38 | +@Override |
| 39 | +publicvoidsetUpGlobal()throwsException { |
| 40 | + |
| 41 | +server =newServer(); |
| 42 | +ServerConnectorconnector1 =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 | +publicAbstractHandlerconfigureHandler()throwsException { |
| 51 | +returnnewBasicAuthTest.SimpleHandler(); |
| 52 | + } |
| 53 | + |
| 54 | +@Test(groups ="standalone",enabled =false) |
| 55 | +publicvoidtestNoRealm()throwsException { |
| 56 | +Filefile =createTempFile(1024 *1024); |
| 57 | + |
| 58 | +try (AsyncHttpClientclient =asyncHttpClient()) { |
| 59 | +for (inti =0;i <20;i++) { |
| 60 | +Responseresponse =client.preparePut(getTargetUrl())// |
| 61 | + .addBodyPart(newFilePart("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 | +publicvoidtestAuthorizedRealm()throwsException { |
| 69 | +Filefile =createTempFile(1024 *1024); |
| 70 | + |
| 71 | +try (AsyncHttpClientclient =asyncHttpClient()) { |
| 72 | +for (inti =0;i <20;i++) { |
| 73 | +Responseresponse =client.preparePut(getTargetUrl())// |
| 74 | + .setRealm(basicAuthRealm(USER,ADMIN).build())// |
| 75 | + .addBodyPart(newFilePart("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 | +} |