下面列出了org.apache.http.impl.io.EmptyInputStream#com.amazonaws.http.AmazonHttpClient 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Before
public void setUp() throws IOException {
AWSCredentialsProvider credentials = new AWSStaticCredentialsProvider(new BasicAWSCredentials("foo", "bar"));
mockClient = Mockito.mock(SdkHttpClient.class);
HttpResponse resp = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
BasicHttpEntity entity = new BasicHttpEntity();
entity.setContent(new ByteArrayInputStream("test payload".getBytes()));
resp.setEntity(entity);
Mockito.doReturn(resp).when(mockClient).execute(any(HttpUriRequest.class), any(HttpContext.class));
ClientConfiguration clientConfig = new ClientConfiguration();
client = new GenericApiGatewayClientBuilder()
.withClientConfiguration(clientConfig)
.withCredentials(credentials)
.withEndpoint("https://foobar.execute-api.us-east-1.amazonaws.com")
.withRegion(Region.getRegion(Regions.fromName("us-east-1")))
.withApiKey("12345")
.withHttpClient(new AmazonHttpClient(clientConfig, mockClient, null))
.build();
}
@Test
public void testExecute_noApiKey_noCreds() throws IOException {
client = new GenericApiGatewayClientBuilder()
.withEndpoint("https://foobar.execute-api.us-east-1.amazonaws.com")
.withRegion(Region.getRegion(Regions.fromName("us-east-1")))
.withClientConfiguration(new ClientConfiguration())
.withHttpClient(new AmazonHttpClient(new ClientConfiguration(), mockClient, null))
.build();
GenericApiGatewayResponse response = client.execute(
new GenericApiGatewayRequestBuilder()
.withBody(new ByteArrayInputStream("test request".getBytes()))
.withHttpMethod(HttpMethodName.POST)
.withResourcePath("/test/orders").build());
assertEquals("Wrong response body", "test payload", response.getBody());
assertEquals("Wrong response status", 200, response.getHttpResponse().getStatusCode());
Mockito.verify(mockClient, times(1)).execute(argThat(new LambdaMatcher<>(
x -> (x.getMethod().equals("POST")
&& x.getFirstHeader("x-api-key") == null
&& x.getFirstHeader("Authorization") == null
&& x.getURI().toString().equals("https://foobar.execute-api.us-east-1.amazonaws.com/test/orders")))),
any(HttpContext.class));
}
@Before
public void setUp() throws Exception {
mockSdkClient = Mockito.mock(SdkHttpClient.class);
ClientConfiguration clientConfig = new ClientConfiguration();
mockGetApi = new InvokeAWSGatewayApi(
new AmazonHttpClient(clientConfig, mockSdkClient, null));
runner = TestRunners.newTestRunner(mockGetApi);
runner.setValidateExpressionUsage(false);
final AWSCredentialsProviderControllerService serviceImpl = new AWSCredentialsProviderControllerService();
runner.addControllerService("awsCredentialsProvider", serviceImpl);
runner.setProperty(serviceImpl, InvokeAWSGatewayApi.ACCESS_KEY, "awsAccessKey");
runner.setProperty(serviceImpl, InvokeAWSGatewayApi.SECRET_KEY, "awsSecretKey");
runner.enableControllerService(serviceImpl);
runner.setProperty(InvokeAWSGatewayApi.AWS_CREDENTIALS_PROVIDER_SERVICE,
"awsCredentialsProvider");
runner.setProperty(InvokeAWSGatewayApi.PROP_AWS_GATEWAY_API_REGION, "us-east-1");
runner.setProperty(InvokeAWSGatewayApi.PROP_AWS_API_KEY, "abcd");
runner.setProperty(InvokeAWSGatewayApi.PROP_RESOURCE_NAME, "/TEST");
runner.setProperty(InvokeAWSGatewayApi.PROP_AWS_GATEWAY_API_ENDPOINT,
"https://foobar.execute-api.us-east-1.amazonaws.com");
}
private static RequestExecutionBuilder buildRequest(final Request request) {
try {
return new AmazonHttpClient(new ClientConfiguration()).requestExecutionBuilder()
.executionContext(new ExecutionContext(true)).request(request)
.errorResponseHandler(new ErrorResponseHandler(false));
} catch (AmazonServiceException exception) {
System.out.println("Unexpected status code in response: " + exception.getStatusCode());
System.out.println("Content: " + exception.getRawResponseContent());
throw new RuntimeException("Failed request. Aborting.");
}
}
GenericApiGatewayClient(ClientConfiguration clientConfiguration, String endpoint, Region region,
AWSCredentialsProvider credentials, String apiKey, AmazonHttpClient httpClient) {
super(clientConfiguration);
setRegion(region);
setEndpoint(endpoint);
this.credentials = credentials;
this.apiKey = apiKey;
this.signer = new AWS4Signer();
this.signer.setServiceName(API_GATEWAY_SERVICE_NAME);
this.signer.setRegionName(region.getName());
final JsonOperationMetadata metadata = new JsonOperationMetadata().withHasStreamingSuccessResponse(false).withPayloadJson(false);
final Unmarshaller<GenericApiGatewayResponse, JsonUnmarshallerContext> responseUnmarshaller = in -> new GenericApiGatewayResponse(in.getHttpResponse());
this.responseHandler = SdkStructuredPlainJsonFactory.SDK_JSON_FACTORY.createResponseHandler(metadata, responseUnmarshaller);
JsonErrorUnmarshaller defaultErrorUnmarshaller = new JsonErrorUnmarshaller(GenericApiGatewayException.class, null) {
@Override
public AmazonServiceException unmarshall(JsonNode jsonContent) throws Exception {
return new GenericApiGatewayException(jsonContent.toString());
}
};
this.errorResponseHandler = SdkStructuredPlainJsonFactory.SDK_JSON_FACTORY.createErrorResponseHandler(
Collections.singletonList(defaultErrorUnmarshaller), null);
if (httpClient != null) {
super.client = httpClient;
}
}
/**
* Perform this request.
*/
@Override
public T perform() {
final Response<T> rsp = new AmazonHttpClient(new ClientConfiguration())
.requestExecutionBuilder()
.executionContext(new ExecutionContext(true))
.request(this.request)
.errorResponseHandler(this.errHandler)
.execute(this.respHandler);
return rsp.getAwsResponse();
}
GenericApiGatewayClient(ClientConfiguration clientConfiguration, String endpoint, Region region,
AWSCredentialsProvider credentials, String apiKey, AmazonHttpClient httpClient) {
super(clientConfiguration);
setRegion(region);
setEndpoint(endpoint);
this.credentials = credentials;
this.apiKey = apiKey;
this.signer = new AWS4Signer();
this.signer.setServiceName(API_GATEWAY_SERVICE_NAME);
this.signer.setRegionName(region.getName());
final JsonOperationMetadata metadata = new JsonOperationMetadata().withHasStreamingSuccessResponse(false).withPayloadJson(false);
final Unmarshaller<GenericApiGatewayResponse, JsonUnmarshallerContext> responseUnmarshaller = in -> new GenericApiGatewayResponse(in.getHttpResponse());
this.responseHandler = SdkStructuredPlainJsonFactory.SDK_JSON_FACTORY.createResponseHandler(metadata, responseUnmarshaller);
JsonErrorUnmarshaller defaultErrorUnmarshaller = new JsonErrorUnmarshaller(GenericApiGatewayException.class, null) {
@Override
public AmazonServiceException unmarshall(JsonNode jsonContent) throws Exception {
return new GenericApiGatewayException(jsonContent.toString());
}
};
this.errorResponseHandler = SdkStructuredPlainJsonFactory.SDK_JSON_FACTORY.createErrorResponseHandler(
Collections.singletonList(defaultErrorUnmarshaller), null);
if (httpClient != null) {
super.client = httpClient;
}
}
public GenericApiGatewayClientBuilder withHttpClient(AmazonHttpClient client) {
this.httpClient = client;
return this;
}
public AmazonHttpClient getHttpClient() {
return httpClient;
}
public InvokeAWSGatewayApi(AmazonHttpClient client) {
super(client);
}
public GenericApiGatewayClientBuilder withHttpClient(AmazonHttpClient client) {
this.httpClient = client;
return this;
}
public AmazonHttpClient getHttpClient() {
return httpClient;
}
public AbstractAWSGatewayApiProcessor(AmazonHttpClient client) {
providedClient = client;
}