implemented bindChallenge

feature/mock
Anders Mikkelsen 2019-10-26 19:22:51 +02:00 committed by Richard Körber
parent 0343a81a9f
commit 0b1e512a57
2 changed files with 55 additions and 0 deletions

View File

@ -23,6 +23,8 @@ import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;
import org.shredzone.acme4j.challenge.Challenge; import org.shredzone.acme4j.challenge.Challenge;
import org.shredzone.acme4j.connector.Connection;
import org.shredzone.acme4j.exception.AcmeException;
import org.shredzone.acme4j.exception.AcmeProtocolException; import org.shredzone.acme4j.exception.AcmeProtocolException;
import org.shredzone.acme4j.toolbox.JSON; import org.shredzone.acme4j.toolbox.JSON;
@ -123,6 +125,21 @@ public class Login {
return new Order(this, requireNonNull(location, "location")); return new Order(this, requireNonNull(location, "location"));
} }
/**
* Creates a new instance of {@link Challenge} and binds it to this login.
*
* @param location
* Location URL of the order
* @return {@link Challenge} bound to the login
*/
public Challenge bindChallenge(URL location) throws AcmeException {
Connection connect = session.connect();
connect.sendSignedPostAsGetRequest(location, this);
JSON data = connect.readJsonResponse();
Objects.requireNonNull(data, "data");
return createChallenge(data);
}
/** /**
* Creates a {@link Challenge} instance for the given challenge data. * Creates a {@link Challenge} instance for the given challenge data.
* *

View File

@ -16,9 +16,11 @@ package org.shredzone.acme4j;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import static org.shredzone.acme4j.toolbox.TestUtils.getJSON;
import static org.shredzone.acme4j.toolbox.TestUtils.url; import static org.shredzone.acme4j.toolbox.TestUtils.url;
import java.io.IOException; import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.security.KeyPair; import java.security.KeyPair;
@ -27,6 +29,7 @@ import org.mockito.ArgumentMatchers;
import org.shredzone.acme4j.challenge.Challenge; import org.shredzone.acme4j.challenge.Challenge;
import org.shredzone.acme4j.challenge.Http01Challenge; import org.shredzone.acme4j.challenge.Http01Challenge;
import org.shredzone.acme4j.provider.AcmeProvider; import org.shredzone.acme4j.provider.AcmeProvider;
import org.shredzone.acme4j.provider.TestableConnectionProvider;
import org.shredzone.acme4j.toolbox.JSON; import org.shredzone.acme4j.toolbox.JSON;
import org.shredzone.acme4j.toolbox.JSONBuilder; import org.shredzone.acme4j.toolbox.JSONBuilder;
import org.shredzone.acme4j.toolbox.TestUtils; import org.shredzone.acme4j.toolbox.TestUtils;
@ -135,4 +138,39 @@ public class LoginTest {
verify(mockProvider).createChallenge(login, data); verify(mockProvider).createChallenge(login, data);
} }
/**
* Test that binding to a challenge invokes createChallenge
*/
@Test
public void testBindChallenge() throws Exception {
URL locationUrl = new URL("https://example.com/acme/challenge/1");
Http01Challenge mockChallenge = mock(Http01Challenge.class);
JSON httpChallenge = getJSON("httpChallenge");
TestableConnectionProvider provider = new TestableConnectionProvider() {
@Override
public int sendSignedPostAsGetRequest(URL url, Login login) {
assertThat(url, is(locationUrl));
return HttpURLConnection.HTTP_OK;
}
@Override
public JSON readJsonResponse() {
return httpChallenge;
}
@Override
public Challenge createChallenge(Login login, JSON json) {
assertThat(json, is(httpChallenge));
return mockChallenge;
}
};
Login login = provider.createLogin();
Challenge challenge = login.bindChallenge(locationUrl);
assertThat(challenge, is(instanceOf(Http01Challenge.class)));
assertThat(challenge, is(sameInstance(mockChallenge)));
}
} }