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

使用Hoverfly和Java进行测试的第4部分:精确,Glob和正则表达式匹配器以前,我们在其状态功能中使用了Hoverfly。到目前为止,我们的示例已接近绝对请求匹配,因此在此博客上,我们将重点介绍利用匹配器。拥有广泛的匹配器非常重要,因为大多数API交互都是动态的,您不能总是预测示例。想象一下JWT签名。您可以匹配主体,但签名可能会因环境而异。共有三种类型的匹配器。完全匹配:字段标题应完全匹配全局匹配器:一种匹配项,可以使用`*`正则表达式匹…

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

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

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

共有三种类型的匹配器。

  • 完全匹配:字段标题应完全匹配
  • 全局匹配器:一种匹配项,可以使用`*`
  • 正则表达式匹配器:一种匹配器,要求您在互联网上再次搜索如何制作正则表达式
  • 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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/34155.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注