View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.http.response;
6   
7   import com.jcabi.http.request.FakeRequest;
8   import java.net.URI;
9   import org.hamcrest.MatcherAssert;
10  import org.hamcrest.Matchers;
11  import org.junit.jupiter.api.Test;
12  
13  /**
14   * Test case for {@link WebLinkingResponse}.
15   * @since 0.9
16   */
17  final class WebLinkingResponseTest {
18  
19      /**
20       * The Link header.
21       */
22      private static final String LINK = "Link";
23  
24      /**
25       * WebLinkingResponse can recognize Links in headers.
26       * @throws Exception If something goes wrong inside
27       */
28      @Test
29      @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
30      void parsesLinksInHeaders() throws Exception {
31          final String[] headers = {
32              "</hey/foo>; title=\"Hi!\"; rel=foo",
33              "</hey/foo>; title=\"\u20ac\"; rel=\"foo\"; media=\"text/xml\"",
34          };
35          for (final String header : headers) {
36              final WebLinkingResponse response = new WebLinkingResponse(
37                  new FakeRequest()
38                      .withHeader(WebLinkingResponseTest.LINK, header)
39                      .fetch()
40              );
41              final WebLinkingResponse.Link link = response.links().get("foo");
42              MatcherAssert.assertThat(
43                  "should contains '/hey/foo'",
44                  link.uri(),
45                  Matchers.hasToString("/hey/foo")
46              );
47              MatcherAssert.assertThat(
48                  "should contains key 'title'",
49                  link,
50                  Matchers.hasKey("title")
51              );
52              MatcherAssert.assertThat(
53                  "should not contains key 'something else'",
54                  response.links(),
55                  Matchers.not(Matchers.hasKey("something else"))
56              );
57          }
58      }
59  
60      /**
61       * WebLinkingResponse can follow a link.
62       * @throws Exception If something goes wrong inside
63       */
64      @Test
65      void followsLinksInHeaders() throws Exception {
66          final WebLinkingResponse response = new WebLinkingResponse(
67              new FakeRequest().withHeader(
68                  WebLinkingResponseTest.LINK,
69                  "</a>; rel=\"first\", <http://localhost/o>; rel=\"second\""
70              ).uri().set(new URI("http://localhost/test")).back().fetch()
71          );
72          MatcherAssert.assertThat(
73              "should equals 'http://localhost/a'",
74              response.follow("first").uri().get(),
75              Matchers.equalTo(new URI("http://localhost/a"))
76          );
77          MatcherAssert.assertThat(
78              "should equals 'http://localhost/o'",
79              response.follow("second").uri().get(),
80              Matchers.equalTo(new URI("http://localhost/o"))
81          );
82      }
83  
84  }