forked from pact-foundation/pact-jvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PactProviderHttpsKeystoreTest.java
131 lines (115 loc) · 5.11 KB
/
PactProviderHttpsKeystoreTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package au.com.dius.pact.consumer.pactproviderrule;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.SSLHandshakeException;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jayway.restassured.RestAssured;
import au.com.dius.pact.consumer.Pact;
import au.com.dius.pact.consumer.PactProviderRule;
import au.com.dius.pact.consumer.PactVerification;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.exampleclients.ConsumerHttpsClient;
import au.com.dius.pact.model.MockHttpsKeystoreProviderConfig;
import au.com.dius.pact.model.PactFragment;
import au.com.dius.pact.model.PactSpecVersion;
public class PactProviderHttpsKeystoreTest {
private static final Logger LOGGER = LoggerFactory.getLogger(PactProviderHttpsKeystoreTest.class);
@Rule
public PactProviderRule mockTestProvider = new PactProviderRule("test_provider", "localhost", 8447, true,
//Generated jks with the following command:
//keytool -genkeypair -alias localhost -keyalg RSA -validity 36500 -keysize 512 -keystore pact-jvm-512.jks
Paths.get("src/test/resources/keystore/pact-jvm-512.jks").toFile().getAbsolutePath(),"brentwashere", PactSpecVersion.V2, this);
@Pact(provider="test_provider", consumer="test_consumer")
public PactFragment createFragment(PactDslWithProvider builder) {
Map<String, String> headers = new HashMap<String, String>();
headers.put("testreqheader", "testreqheadervalue");
Map<String, String> jsonResponseHeaders = new HashMap<String, String>();
jsonResponseHeaders.put("Content-Type", "application/json");
jsonResponseHeaders.put("testreqheader", "testreqheadervalue");
return builder
.given("good state")
.uponReceiving("PactProviderTest test interaction")
.path("/")
.method("GET")
.headers(headers)
.willRespondWith()
.status(200)
.headers(jsonResponseHeaders)
.body("{\"responsetest\": true, \"name\": \"harry\"}")
.uponReceiving("PactProviderTest second test interaction")
.method("OPTIONS")
.headers(headers)
.path("/second")
.body("")
.willRespondWith()
.status(200)
.headers(headers)
.body("")
.toFragment();
}
@Test
@PactVerification(value = "test_provider")
public void testKeystoreHappyPath() {
MockHttpsKeystoreProviderConfig config = (MockHttpsKeystoreProviderConfig) mockTestProvider.getConfig();
LOGGER.info("Keystore path: " + config.getKeystore());
RestAssured
.given()
.header("testreqheader", "testreqheadervalue")
.trustStore(config.getKeystore(), config.getKeystorePassword())
.when()
.options(mockTestProvider.getConfig().url() + "/second")
.then()
.statusCode(200);
RestAssured
.given()
.header("testreqheader", "testreqheadervalue")
.trustStore(config.getKeystore(), config.getKeystorePassword())
.when()
.get(mockTestProvider.getConfig().url() + "/")
.then()
.body("responsetest", Matchers.equalTo(true))
.body("name", Matchers.equalTo("harry"));
}
@Test(expected = SSLHandshakeException.class)
@PactVerification(value = "test_provider")
public void testSslHandshakeException() throws IOException {
testKeystoreHappyPath();
new ConsumerHttpsClient(mockTestProvider.getConfig().url()).getAsMap("/", "");
}
@Test(expected = SSLHandshakeException.class)
@PactVerification(value = "test_provider")
public void testMisMatchedTrustStore() {
testKeystoreHappyPath();
//Used the following command to create jks file:
//keytool -genkeypair -alias localhost -keyalg RSA -validity 36500 -keystore pact-jvm-other.jks
File trustStore = Paths.get("src/test/resources/keystore/pact-jvm-other.jks").toFile();
RestAssured
.given()
.header("testreqheader", "testreqheadervalue")
.trustStore(trustStore, "bbarkewashere")
.when()
.options(mockTestProvider.getConfig().url() + "/second")
.then()
.statusCode(200);
}
}