View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.http.mock;
6   
7   import java.util.Collections;
8   import org.hamcrest.MatcherAssert;
9   import org.hamcrest.Matchers;
10  import org.junit.jupiter.api.Test;
11  import org.mockito.Mockito;
12  
13  /**
14   * Test case for {@link MkAnswerMatchers}.
15   * @since 1.5
16   */
17  final class MkAnswerMatchersTest {
18  
19      /**
20       * MkAnswerMatchers should be able to match MkAnswer body.
21       */
22      @Test
23      void canMatchBody() {
24          final String body = "Hello \u20ac!";
25          final MkAnswer query = Mockito.mock(MkAnswer.class);
26          Mockito.doReturn(body).when(query).body();
27          MatcherAssert.assertThat(
28              "should match the answer body",
29              query,
30              MkAnswerMatchers.hasBody(
31                  Matchers.is(body)
32              )
33          );
34      }
35  
36      /**
37       * MkAnswerMatchers can match MkAnswer body bytes.
38       */
39      @Test
40      void canMatchBodyBytes() {
41          final byte[] body = {0x01, 0x45, 0x21};
42          final MkAnswer query = Mockito.mock(MkAnswer.class);
43          Mockito.doReturn(body).when(query).bodyBytes();
44          MatcherAssert.assertThat(
45              "should match the answer body bytes",
46              query,
47              MkAnswerMatchers.hasBodyBytes(
48                  Matchers.is(body)
49              )
50          );
51      }
52  
53      /**
54       * MkAnswerMatchers should be able to match MkAnswer header.
55       */
56      @Test
57      void canMatchHeader() {
58          final String header = "Content-Type";
59          final String value = "application/json";
60          final MkAnswer query = Mockito.mock(MkAnswer.class);
61          Mockito.doReturn(
62              Collections.singletonMap(header, Collections.singletonList(value))
63          ).when(query).headers();
64          MatcherAssert.assertThat(
65              "should match the answer header",
66              query,
67              MkAnswerMatchers.hasHeader(
68                  header,
69                  Matchers.contains(value)
70              )
71          );
72      }
73  }