Search in sources :

Example 1 with AuthenticationException

use of org.mockserver.authentication.AuthenticationException in project mockserver by mock-server.

the class AuthenticatedControlPlaneUsingMTLSClientNotAuthenticatedIntegrationTest method clientOperationIsAuthenticated.

private void clientOperationIsAuthenticated(ThrowingRunnable throwingRunnable) {
    // when
    AuthenticationException authenticationException = assertThrows(AuthenticationException.class, throwingRunnable);
    // then
    assertThat(authenticationException.getMessage(), equalTo("Unauthorized for control plane - control plane request failed authentication no client certificates can be validated by control plane CA"));
}
Also used : AuthenticationException(org.mockserver.authentication.AuthenticationException)

Example 2 with AuthenticationException

use of org.mockserver.authentication.AuthenticationException in project mockserver by mock-server.

the class MockServerClient method verify.

/**
 * Verify a list of requests have been sent in the order specified for example:
 * <pre>
 * mockServerClient
 *  .verify(
 *      request()
 *          .withPath("/first_request")
 *          .withBody("some_request_body"),
 *      request()
 *          .withPath("/second_request")
 *          .withBody("some_request_body")
 *  );
 * </pre>
 *
 * @param expectationIds the http requests that must be matched for this verification to pass
 * @throws AssertionError if the request has not been found
 */
public MockServerClient verify(ExpectationId... expectationIds) throws AssertionError {
    if (expectationIds == null || expectationIds.length == 0 || expectationIds[0] == null) {
        throw new IllegalArgumentException("verify(RequestDefinition...) requires a non-null non-empty array of RequestDefinition objects");
    }
    try {
        VerificationSequence verificationSequence = new VerificationSequence().withExpectationIds(expectationIds);
        String result = sendRequest(request().withMethod("PUT").withContentType(APPLICATION_JSON_UTF_8).withPath(calculatePath("verifySequence")).withBody(verificationSequenceSerializer.serialize(verificationSequence), StandardCharsets.UTF_8)).getBodyAsString();
        if (result != null && !result.isEmpty()) {
            throw new AssertionError(result);
        }
    } catch (AuthenticationException authenticationException) {
        throw authenticationException;
    } catch (Throwable throwable) {
        throw new AssertionError(throwable.getMessage());
    }
    return clientClass.cast(this);
}
Also used : AuthenticationException(org.mockserver.authentication.AuthenticationException) VerificationSequence(org.mockserver.verify.VerificationSequence)

Example 3 with AuthenticationException

use of org.mockserver.authentication.AuthenticationException in project mockserver by mock-server.

the class MockServerClient method verify.

/**
 * Verify a request has been sent for example:
 * <pre>
 * mockServerClient
 *  .verify(
 *      request()
 *          .withPath("/some_path")
 *          .withBody("some_request_body"),
 *      VerificationTimes.exactly(3)
 *  );
 * </pre>
 * VerificationTimes supports multiple static factory methods:
 * <p>
 * once()      - verify the request was only received once
 * exactly(n)  - verify the request was only received exactly n times
 * atLeast(n)  - verify the request was only received at least n times
 *
 * @param requestDefinition the http request that must be matched for this verification to pass
 * @param times             the number of times this request must be matched
 * @throws AssertionError if the request has not been found
 */
@SuppressWarnings("DuplicatedCode")
public MockServerClient verify(RequestDefinition requestDefinition, VerificationTimes times) throws AssertionError {
    if (requestDefinition == null) {
        throw new IllegalArgumentException("verify(RequestDefinition, VerificationTimes) requires a non null RequestDefinition object");
    }
    if (times == null) {
        throw new IllegalArgumentException("verify(RequestDefinition, VerificationTimes) requires a non null VerificationTimes object");
    }
    try {
        Verification verification = verification().withRequest(requestDefinition).withTimes(times);
        String result = sendRequest(request().withMethod("PUT").withContentType(APPLICATION_JSON_UTF_8).withPath(calculatePath("verify")).withBody(verificationSerializer.serialize(verification), StandardCharsets.UTF_8)).getBodyAsString();
        if (result != null && !result.isEmpty()) {
            throw new AssertionError(result);
        }
    } catch (AuthenticationException authenticationException) {
        throw authenticationException;
    } catch (Throwable throwable) {
        throw new AssertionError(throwable.getMessage());
    }
    return clientClass.cast(this);
}
Also used : AuthenticationException(org.mockserver.authentication.AuthenticationException) Verification(org.mockserver.verify.Verification)

Example 4 with AuthenticationException

use of org.mockserver.authentication.AuthenticationException in project mockserver by mock-server.

the class MockServerClient method verify.

/**
 * Verify a list of requests have been sent in the order specified for example:
 * <pre>
 * mockServerClient
 *  .verify(
 *      request()
 *          .withPath("/first_request")
 *          .withBody("some_request_body"),
 *      request()
 *          .withPath("/second_request")
 *          .withBody("some_request_body")
 *  );
 * </pre>
 *
 * @param requestDefinitions the http requests that must be matched for this verification to pass
 * @throws AssertionError if the request has not been found
 */
public MockServerClient verify(RequestDefinition... requestDefinitions) throws AssertionError {
    if (requestDefinitions == null || requestDefinitions.length == 0 || requestDefinitions[0] == null) {
        throw new IllegalArgumentException("verify(RequestDefinition...) requires a non-null non-empty array of RequestDefinition objects");
    }
    try {
        VerificationSequence verificationSequence = new VerificationSequence().withRequests(requestDefinitions);
        String result = sendRequest(request().withMethod("PUT").withContentType(APPLICATION_JSON_UTF_8).withPath(calculatePath("verifySequence")).withBody(verificationSequenceSerializer.serialize(verificationSequence), StandardCharsets.UTF_8)).getBodyAsString();
        if (result != null && !result.isEmpty()) {
            throw new AssertionError(result);
        }
    } catch (AuthenticationException authenticationException) {
        throw authenticationException;
    } catch (Throwable throwable) {
        throw new AssertionError(throwable.getMessage());
    }
    return clientClass.cast(this);
}
Also used : AuthenticationException(org.mockserver.authentication.AuthenticationException) VerificationSequence(org.mockserver.verify.VerificationSequence)

Example 5 with AuthenticationException

use of org.mockserver.authentication.AuthenticationException in project mockserver by mock-server.

the class MockServerClient method verify.

/**
 * Verify a request has been sent for example:
 * <pre>
 * mockServerClient
 *  .verify(
 *      request()
 *          .withPath("/some_path")
 *          .withBody("some_request_body"),
 *      VerificationTimes.exactly(3)
 *  );
 * </pre>
 * VerificationTimes supports multiple static factory methods:
 * <p>
 * once()      - verify the request was only received once
 * exactly(n)  - verify the request was only received exactly n times
 * atLeast(n)  - verify the request was only received at least n times
 *
 * @param expectationId the http request that must be matched for this verification to pass
 * @param times         the number of times this request must be matched
 * @throws AssertionError if the request has not been found
 */
@SuppressWarnings("DuplicatedCode")
public MockServerClient verify(ExpectationId expectationId, VerificationTimes times) throws AssertionError {
    if (expectationId == null) {
        throw new IllegalArgumentException("verify(RequestDefinition, VerificationTimes) requires a non null RequestDefinition object");
    }
    if (times == null) {
        throw new IllegalArgumentException("verify(RequestDefinition, VerificationTimes) requires a non null VerificationTimes object");
    }
    try {
        Verification verification = verification().withExpectationId(expectationId).withTimes(times);
        String result = sendRequest(request().withMethod("PUT").withContentType(APPLICATION_JSON_UTF_8).withPath(calculatePath("verify")).withBody(verificationSerializer.serialize(verification), StandardCharsets.UTF_8)).getBodyAsString();
        if (result != null && !result.isEmpty()) {
            throw new AssertionError(result);
        }
    } catch (AuthenticationException authenticationException) {
        throw authenticationException;
    } catch (Throwable throwable) {
        throw new AssertionError(throwable.getMessage());
    }
    return clientClass.cast(this);
}
Also used : AuthenticationException(org.mockserver.authentication.AuthenticationException) Verification(org.mockserver.verify.Verification)

Aggregations

AuthenticationException (org.mockserver.authentication.AuthenticationException)34 Test (org.junit.Test)19 AuthenticationHandler (org.mockserver.authentication.AuthenticationHandler)13 HttpRequest (org.mockserver.model.HttpRequest)13 AsymmetricKeyPair (org.mockserver.keys.AsymmetricKeyPair)8 X509Certificate (java.security.cert.X509Certificate)6 JWTGenerator (org.mockserver.authentication.jwt.JWTGenerator)5 JDKCertificateToMockServerX509Certificate (org.mockserver.mappers.JDKCertificateToMockServerX509Certificate)5 Verification (org.mockserver.verify.Verification)5 VerificationSequence (org.mockserver.verify.VerificationSequence)4 ArrayList (java.util.ArrayList)2 JOSEException (com.nimbusds.jose.JOSEException)1 BadJOSEException (com.nimbusds.jose.proc.BadJOSEException)1 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)1 ParseException (java.text.ParseException)1 HashSet (java.util.HashSet)1 Supplier (java.util.function.Supplier)1 LogEntry (org.mockserver.log.model.LogEntry)1