使用Hoverfly和Java进行测试的第4部分:精确,Glob和正则表达式匹配器

以前,我们在其状态功能中使用了Hoverfly。到目前为止,我们的示例已接近绝对请求匹配,因此在此博客上,我们将重点介绍利用匹配器。拥有广泛的匹配器非常重要,因为大多数API交互都是动态的,您不能总是预测示例。 想象一下JWT签名。 您可以匹配主体,但签名可能会因环境而异。

共有三种类型的匹配器。

  • 完全匹配:字段标题应完全匹配
  • 全局匹配器:一种匹配项,可以使用`*`
  • 正则表达式匹配器:一种匹配器,要求您在互联网上再次搜索如何制作正则表达式
  • XML匹配器:这涉及将XML作为XML节点(逐个节点,逐个值)进行匹配
  • Xpath匹配器:基于通过Xpath匹配的值进行匹配
  • JSON匹配器:完全匹配JSON
  • JSON部分匹配器:如果提交的json包含指定的Json值,则匹配
  • JSONPath匹配器:就像基于提交的json路径的xpath匹配一样

让我们从确切的匹配器开始。

 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());

    }
 }

失败或成功取决于报头是否完全匹配。

我们将对请求参数使用全局匹配。

 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());

    }
 }

最后,让我们前往正则表达式匹配器。 正则表达式匹配器将只检查大写字母:([AZ])\ w +

 public class RegexMatcherTests { 
    private Hoverfly hoverfly; 
    @BeforeEach

    void setUp() {

        var simulation = SimulationSource.dsl(service( " http://localhost:8085 " )

                .post( "/regex" )

                .body(RequestFieldMatcher.newRegexMatcher( "([AZ])\\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());

    }
 }

就是这样,我们确实将基本匹配器用于精确,基于glob和正则表达式。 下一个博客将重点介绍基于xml的匹配器。

翻译自: https://www.javacodegeeks.com/2020/11/testing-with-hoverfly-and-java-part-4-exact-glob-and-regex-matchers.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值