1
2
3
4
5 package com.jcabi.http.wire;
6
7 import com.jcabi.http.mock.MkAnswer;
8 import com.jcabi.http.mock.MkContainer;
9 import com.jcabi.http.mock.MkGrizzlyContainer;
10 import com.jcabi.http.mock.MkQuery;
11 import com.jcabi.http.request.JdkRequest;
12 import com.jcabi.http.response.RestResponse;
13 import jakarta.ws.rs.core.HttpHeaders;
14 import org.apache.http.HttpStatus;
15 import org.hamcrest.MatcherAssert;
16 import org.hamcrest.Matchers;
17 import org.junit.jupiter.api.Test;
18
19
20
21
22
23
24 final class AutoRedirectingWireTest {
25
26
27
28
29
30
31
32 @Test
33 void retriesForHttpRedirectStatus() throws Exception {
34 final MkContainer container = new MkGrizzlyContainer().next(
35 new MkAnswer.Simple(HttpStatus.SC_MOVED_TEMPORARILY, "")
36
37 .withHeader(HttpHeaders.LOCATION, "/"),
38 Matchers.any(MkQuery.class),
39 Integer.MAX_VALUE
40 ).start();
41 try {
42 final int retries = 3;
43 new JdkRequest(container.home())
44 .through(AutoRedirectingWire.class, retries)
45 .fetch().as(RestResponse.class)
46 .assertStatus(HttpStatus.SC_MOVED_TEMPORARILY);
47 MatcherAssert.assertThat(
48 "should retries 3 times",
49 container.takeAll(Matchers.any(MkAnswer.class)),
50 Matchers.<MkQuery>iterableWithSize(retries)
51 );
52 } finally {
53 container.stop();
54 }
55 }
56
57
58
59
60
61
62 @Test
63 void returnsValidResponseAfterRetry() throws Exception {
64 final String body = "success";
65 final MkContainer container = new MkGrizzlyContainer().next(
66 new MkAnswer.Simple(HttpStatus.SC_MOVED_TEMPORARILY, "")
67 .withHeader(HttpHeaders.LOCATION, "/"),
68 Matchers.any(MkQuery.class),
69 2
70 ).next(new MkAnswer.Simple(body)).start();
71 try {
72 new JdkRequest(container.home())
73 .through(AutoRedirectingWire.class)
74 .fetch().as(RestResponse.class)
75 .assertBody(Matchers.is(body))
76 .assertStatus(HttpStatus.SC_OK);
77 MatcherAssert.assertThat(
78 "should retries 3 times",
79 container.takeAll(Matchers.any(MkAnswer.class)),
80 Matchers.<MkQuery>iterableWithSize(3)
81 );
82 } finally {
83 container.stop();
84 }
85 }
86
87 }