1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.http.response;
31
32 import com.jcabi.http.request.FakeRequest;
33 import java.net.URI;
34 import org.hamcrest.MatcherAssert;
35 import org.hamcrest.Matchers;
36 import org.junit.jupiter.api.Test;
37
38
39
40
41
42 final class WebLinkingResponseTest {
43
44
45
46
47 private static final String LINK = "Link";
48
49
50
51
52
53 @Test
54 @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
55 void parsesLinksInHeaders() throws Exception {
56 final String[] headers = {
57 "</hey/foo>; title=\"Hi!\"; rel=foo",
58 "</hey/foo>; title=\"\u20ac\"; rel=\"foo\"; media=\"text/xml\"",
59 };
60 for (final String header : headers) {
61 final WebLinkingResponse response = new WebLinkingResponse(
62 new FakeRequest()
63 .withHeader(WebLinkingResponseTest.LINK, header)
64 .fetch()
65 );
66 final WebLinkingResponse.Link link = response.links().get("foo");
67 MatcherAssert.assertThat(
68 link.uri(),
69 Matchers.hasToString("/hey/foo")
70 );
71 MatcherAssert.assertThat(
72 link,
73 Matchers.hasKey("title")
74 );
75 MatcherAssert.assertThat(
76 response.links(),
77 Matchers.not(Matchers.hasKey("something else"))
78 );
79 }
80 }
81
82
83
84
85
86 @Test
87 void followsLinksInHeaders() throws Exception {
88 final WebLinkingResponse response = new WebLinkingResponse(
89 new FakeRequest().withHeader(
90 WebLinkingResponseTest.LINK,
91 "</a>; rel=\"first\", <http://localhost/o>; rel=\"second\""
92 ).uri().set(new URI("http://localhost/test")).back().fetch()
93 );
94 MatcherAssert.assertThat(
95 response.follow("first").uri().get(),
96 Matchers.equalTo(new URI("http://localhost/a"))
97 );
98 MatcherAssert.assertThat(
99 response.follow("second").uri().get(),
100 Matchers.equalTo(new URI("http://localhost/o"))
101 );
102 }
103
104 }