Core Java

Testing with Hoverfly and Java Part 4: Exact, Glob and Regex Matchers

Previously we used Hoverfly among its state feature.
So far our examples have been close to an absolute request match, thus on this blog we will focus on utilising the matchers.
Having a good range of matchers is very important because most API interactions are dynamic and you can’t always predict the example. Imagine a JWT signature. You can match the body but the signature might change per environment.

There are three type of matchers available.

  • The exact matcher: The fields headers should be an exact match
  • The glob matcher: A match that gives the ability to using the `*`
  • The regex matcher: A matcher that requires you to search again on the internet on how to make a regex
  • XML matcher: This about matching the xml as XML node by node value by value
  • Xpath matcher: Match based on a value match through Xpath
  • JSON matcher: Match the json exactly
  • JSON partial matcher: Match if the json submitted contains the Json values specified
  • JSONPath matcher: Just like the xpath match based the on the json path submitted

Let’s start with the exact matcher.

01
02
03
04
05
06
07
08
09
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
public class ExactMatcherTests {
    private Hoverfly hoverfly;
    @BeforeEach
    void setUp() {
        var simulation = SimulationSource.dsl(service("http://localhost:8085")
                .get("/exact")
                .header("Origin", RequestFieldMatcher.newExactMatcher("internal-server"))
                .willReturn(success("{\"exact\":true}", "application/json")));
        var localConfig = HoverflyConfig.localConfigs().disableTlsVerification().asWebServer().proxyPort(8085);
        hoverfly = new Hoverfly(localConfig, SIMULATE);
        hoverfly.start();
        hoverfly.simulate(simulation);
    }
    @AfterEach
    void tearDown() {
        hoverfly.close();
    }
    @Test
    void testExactMatcherSuccess() {
        var client = HttpClient.newHttpClient();
        var exactRequest = HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:8085/exact"))
                .header("Origin","internal-server")
                .build();
        var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())
                .thenApply(HttpResponse::body)
                .join();
        Assertions.assertEquals("{\"exact\":true}", exactResponse);
    }
    @Test
    void testExactMatcherFailure() {
        var client = HttpClient.newHttpClient();
        var exactRequest = HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:8085/exact"))
                .build();
        var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())
                .join();
        Assertions.assertEquals(502, exactResponse.statusCode());
    }
}

The failures or success come based on wether the header was matching exactly or not.

We shall use the glob match for a request parameter.

01
02
03
04
05
06
07
08
09
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
public class GlobMatcher {
    private Hoverfly hoverfly;
    @BeforeEach
    void setUp() {
        var simulation = SimulationSource.dsl(service("http://localhost:8085")
                .get("/glob")
                .queryParam("userName", RequestFieldMatcher.newGlobMatcher("john*"))
                .willReturn(success("{\"glob\":true}", "application/json")));
        var localConfig = HoverflyConfig.localConfigs().disableTlsVerification().asWebServer().proxyPort(8085);
        hoverfly = new Hoverfly(localConfig, SIMULATE);
        hoverfly.start();
        hoverfly.simulate(simulation);
    }
    @AfterEach
    void tearDown() {
        hoverfly.close();
    }
    @Test
    void testGlobMatcherSuccess() {
        var client = HttpClient.newHttpClient();
        var exactRequest = HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:8085/glob?userName=johnDoe"))
                .build();
        var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())
                .thenApply(HttpResponse::body)
                .join();
        Assertions.assertEquals("{\"glob\":true}", exactResponse);
    }
    @Test
    void testGlobMatcherFailure() {
        var client = HttpClient.newHttpClient();
        var exactRequest = HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:8085/glob?userName=nojohnDoe"))
                .build();
        var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())
                .join();
        Assertions.assertEquals(502, exactResponse.statusCode());
    }
}

Last let’s head for the regex matcher. The regex matcher will just check for a capital letter: ([A-Z])\w+

01
02
03
04
05
06
07
08
09
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
public class RegexMatcherTests {
    private Hoverfly hoverfly;
    @BeforeEach
    void setUp() {
        var simulation = SimulationSource.dsl(service("http://localhost:8085")
                .post("/regex")
                .body(RequestFieldMatcher.newRegexMatcher("([A-Z])\\w+"))
                .willReturn(success("{\"regex\":true}", "application/json")));
        var localConfig = HoverflyConfig.localConfigs().disableTlsVerification().asWebServer().proxyPort(8085);
        hoverfly = new Hoverfly(localConfig, SIMULATE);
        hoverfly.start();
        hoverfly.simulate(simulation);
    }
    @AfterEach
    void tearDown() {
        hoverfly.close();
    }
    @Test
    void testRegexMatcherSuccess() {
        var client = HttpClient.newHttpClient();
        var exactRequest = HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:8085/regex"))
                .POST(HttpRequest.BodyPublishers.ofString("Contains capital letter"))
                .build();
        var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())
                .thenApply(HttpResponse::body)
                .join();
        Assertions.assertEquals("{\"regex\":true}", exactResponse);
    }
    @Test
    void testRegexMatcherFailure() {
        var client = HttpClient.newHttpClient();
        var exactRequest = HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:8085/regex"))
                .POST(HttpRequest.BodyPublishers.ofString("won't match due to capital letter missing"))
                .build();
        var exactResponse = client.sendAsync(exactRequest, HttpResponse.BodyHandlers.ofString())
                .join();
        Assertions.assertEquals(502, exactResponse.statusCode());
    }
}

That’s it we did use the basic matchers for exact, glob, and regex based. The next blog shall focus on the xml based matchers.

Published on Java Code Geeks with permission by Emmanouil Gkatziouras, partner at our JCG program. See the original article here: Testing with Hoverfly and Java Part 4: Exact, Glob and Regex Matchers

Opinions expressed by Java Code Geeks contributors are their own.

Emmanouil Gkatziouras

He is a versatile software engineer with experience in a wide variety of applications/services.He is enthusiastic about new projects, embracing new technologies, and getting to know people in the field of software.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button