-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
62 lines (53 loc) · 2.24 KB
/
Server.java
File metadata and controls
62 lines (53 loc) · 2.24 KB
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
package com.octopus.openfeature.provider;
import com.github.tomakehurst.wiremock.WireMockServer;
import java.util.Base64;
import java.util.UUID;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
/**
* Fake HTTP server for specification tests.
*
* Each call to {@link #configure(String)} registers a stub for a unique Bearer token
* and returns that token as the client identifier. Stubs accumulate over the server's
* lifetime (one per test case), which is harmless since each token is unique.
*
* Note: parallel test execution is not supported because SpecificationTests uses
* the OpenFeatureAPI singleton.
*/
class Server {
// A fixed hash is safe here because each test shuts down the provider via OpenFeatureAPI.shutdown()
// before the background refresh thread can poll the check endpoint and compare hashes.
private static final String CONTENT_HASH = Base64.getEncoder().encodeToString(new byte[]{0x01});
private final WireMockServer wireMock;
Server() {
wireMock = new WireMockServer(wireMockConfig().dynamicPort());
wireMock.start();
// Fallback: return 401 for any request that does not match a registered token.
wireMock.stubFor(any(anyUrl())
.atPriority(100)
.willReturn(aResponse().withStatus(401)));
}
/**
* Registers the given JSON as the response body for a new unique client token.
*
* @param responseJson the JSON array that the toggle API would return
* @return the client identifier (Bearer token) to use in OctopusConfiguration
*/
String configure(String responseJson) {
String token = UUID.randomUUID().toString();
wireMock.stubFor(get(urlPathEqualTo("/api/featuretoggles/v3/"))
.withHeader("Authorization", equalTo("Bearer " + token))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withHeader("ContentHash", CONTENT_HASH)
.withBody(responseJson)));
return token;
}
String baseUrl() {
return wireMock.baseUrl();
}
void stop() {
wireMock.stop();
}
}