Java Code Examples for io.micronaut.http.HttpRequest#POST

The following examples show how to use io.micronaut.http.HttpRequest#POST . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommandsTest.java    From java-slack-sdk with MIT License 6 votes vote down vote up
@Test
public void invalidSignature() {
    MutableHttpRequest<String> request = HttpRequest.POST("/slack/events", "");
    request.header("Content-Type", "application/x-www-form-urlencoded");
    String timestamp = "" + (System.currentTimeMillis() / 1000 - 30 * 60);
    request.header(SlackSignature.HeaderNames.X_SLACK_REQUEST_TIMESTAMP, timestamp);
    String signature = signatureGenerator.generate(timestamp, helloBody);
    request.header(SlackSignature.HeaderNames.X_SLACK_SIGNATURE, signature);
    request.body(helloBody);
    try {
        client.toBlocking().exchange(request, String.class);
        Assertions.fail();
    } catch (HttpClientResponseException e) {
        Assertions.assertEquals(401, e.getStatus().getCode());
        Assertions.assertEquals("{\"error\":\"invalid request\"}", e.getResponse().getBody().get());
    }
}
 
Example 2
Source File: AwsLambdaRuntimeApi.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param requestId  Lambda Request Identifier
 * @param errorMessage Error Message
 * @param errorType Error Type
 * @param lambdaFunctionErrorType Lambda Function Error Type
 * @return A request to the invocation error path to inform in JSON format about the error which was thrown during the function execution.
 */
default HttpRequest<AwsLambdaRuntimeApiError> invocationErrorRequest(@Nonnull String requestId,
                                                                    @Nullable String errorMessage,
                                                                    @Nullable String errorType,
                                                                    @Nullable String lambdaFunctionErrorType) {
    AwsLambdaRuntimeApiError error = new AwsLambdaRuntimeApiError(errorMessage, errorType);
    MutableHttpRequest<AwsLambdaRuntimeApiError> request = HttpRequest.POST(errorUri(requestId), error);
    if (lambdaFunctionErrorType != null) {
        return request.header(LAMBDA_RUNTIME_FUNCTION_ERROR_TYPE, lambdaFunctionErrorType);
    }
    return request;
}
 
Example 3
Source File: AwsLambdaRuntimeApi.java    From micronaut-aws with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param errorMessage Error Message
 * @param errorType Error Type
 * @param lambdaFunctionErrorType Lambda Function Error Type
 * @return A post request which should be send if the runtime encounters an error during initialization to post an error message to the initialization error path.
 */
default HttpRequest<AwsLambdaRuntimeApiError> initializationErrorRequest(@Nullable String errorMessage,
                                                                        @Nullable String errorType,
                                                                        @Nullable String lambdaFunctionErrorType) {
    AwsLambdaRuntimeApiError error = new AwsLambdaRuntimeApiError(errorMessage, errorType);
    MutableHttpRequest<AwsLambdaRuntimeApiError> request = HttpRequest.POST(INIT_ERROR_URI, error);
    if (lambdaFunctionErrorType != null) {
        return request.header(LAMBDA_RUNTIME_FUNCTION_ERROR_TYPE, lambdaFunctionErrorType);
    }
    return request;
}
 
Example 4
Source File: LoginTest.java    From micronaut-microservices-poc with Apache License 2.0 5 votes vote down vote up
@Test
public void canLoginWithValidCredentials() {
    UsernamePasswordCredentials upc = new UsernamePasswordCredentials("jimmy.solid","secret");
    HttpRequest loginRequest = HttpRequest.POST("/login", upc);
    HttpResponse<BearerAccessRefreshToken> rsp = httpClient.toBlocking().exchange(loginRequest, BearerAccessRefreshToken.class);
    
    assertThat(rsp.getStatus().getCode()).isEqualTo(200);
    assertThat(rsp.getBody().get().getUsername()).isEqualTo("jimmy.solid");
}
 
Example 5
Source File: LoginTest.java    From micronaut-microservices-poc with Apache License 2.0 5 votes vote down vote up
@Test
public void cantLoginWithInvalidCredentials() {
    try {
        UsernamePasswordCredentials upc = new UsernamePasswordCredentials("jimmy.solid","secret111");
        HttpRequest loginRequest = HttpRequest.POST("/login", upc);
        HttpResponse<BearerAccessRefreshToken> rsp = httpClient.toBlocking().exchange(loginRequest, BearerAccessRefreshToken.class);
        fail();
    } catch (HttpClientResponseException ex) {
        assertThat(ex.getStatus().getCode()).isEqualTo(HttpStatus.UNAUTHORIZED.getCode());
    }
    
}
 
Example 6
Source File: CommandsTest.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Test
public void command() {
    MutableHttpRequest<String> request = HttpRequest.POST("/slack/events", "");
    request.header("Content-Type", "application/x-www-form-urlencoded");
    String timestamp = "" + (System.currentTimeMillis() / 1000);
    request.header(SlackSignature.HeaderNames.X_SLACK_REQUEST_TIMESTAMP, timestamp);
    String signature = signatureGenerator.generate(timestamp, helloBody);
    request.header(SlackSignature.HeaderNames.X_SLACK_SIGNATURE, signature);
    request.body(helloBody);
    HttpResponse<String> response = client.toBlocking().exchange(request, String.class);
    Assertions.assertEquals(200, response.getStatus().getCode());
    Assertions.assertEquals("{\"text\":\"Thanks!\"}", response.getBody().get());
}
 
Example 7
Source File: CommandsTest.java    From java-slack-sdk with MIT License 5 votes vote down vote up
@Test
public void regexp_matching() {
    MutableHttpRequest<String> request = HttpRequest.POST("/slack/events", "");
    request.header("Content-Type", "application/x-www-form-urlencoded");
    String timestamp = "" + (System.currentTimeMillis() / 1000);
    request.header(SlackSignature.HeaderNames.X_SLACK_REQUEST_TIMESTAMP, timestamp);
    String signature = signatureGenerator.generate(timestamp, submissionBody);
    request.header(SlackSignature.HeaderNames.X_SLACK_SIGNATURE, signature);
    request.body(submissionBody);
    HttpResponse<String> response = client.toBlocking().exchange(request, String.class);
    Assertions.assertEquals(200, response.getStatus().getCode());
    Assertions.assertEquals("{\"text\":\"/submission-no.2019\"}", response.getBody().get());
}
 
Example 8
Source File: AwsLambdaRuntimeApi.java    From micronaut-aws with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param requestId AWS Lambda Request ID
 * @param body The body of the request
 * @return Invocation Response Request
 */
default HttpRequest invocationResponseRequest(@Nonnull String requestId, Object body) {
    return HttpRequest.POST(responseUri(requestId), body);
}