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 co.stateful;
31
32 import com.jcabi.http.Request;
33 import com.jcabi.http.mock.MkAnswer;
34 import com.jcabi.http.mock.MkContainer;
35 import com.jcabi.http.mock.MkGrizzlyContainer;
36 import com.jcabi.http.mock.MkQuery;
37 import com.jcabi.http.request.JdkRequest;
38 import java.net.HttpURLConnection;
39 import org.apache.commons.lang3.StringUtils;
40 import org.hamcrest.MatcherAssert;
41 import org.hamcrest.Matchers;
42 import org.junit.Test;
43
44
45
46
47
48
49
50 public final class RtLockTest {
51
52
53
54
55 private static final String XML = StringUtils.join(
56 "<page><links><link rel='lock' href='#lock'/>",
57 "<link rel='unlock' href='#unlock'/></links></page>"
58 );
59
60
61
62
63
64 @Test
65 public void locksViaHttp() throws Exception {
66 final MkContainer container = new MkGrizzlyContainer()
67 .next(new MkAnswer.Simple(RtLockTest.XML))
68 .next(new MkAnswer.Simple(HttpURLConnection.HTTP_SEE_OTHER, ""))
69 .start();
70 final Lock lock = new RtLock("foo", new JdkRequest(container.home()));
71 try {
72 MatcherAssert.assertThat(lock.lock(""), Matchers.equalTo(true));
73 } finally {
74 container.stop();
75 }
76 MatcherAssert.assertThat(
77 container.take().method(),
78 Matchers.equalTo(Request.GET)
79 );
80 final MkQuery query = container.take();
81 MatcherAssert.assertThat(query.method(), Matchers.is(Request.POST));
82 MatcherAssert.assertThat(
83 query.body(), Matchers.containsString("name=foo")
84 );
85 }
86
87
88
89
90
91 @Test
92 public void unlocksViaHttp() throws Exception {
93 final MkContainer container = new MkGrizzlyContainer()
94 .next(new MkAnswer.Simple(RtLockTest.XML))
95 .next(new MkAnswer.Simple(HttpURLConnection.HTTP_SEE_OTHER, ""))
96 .start();
97 final Lock lock = new RtLock("", new JdkRequest(container.home()));
98 try {
99 lock.unlock("");
100 } finally {
101 container.stop();
102 }
103 MatcherAssert.assertThat(
104 container.take().method(),
105 Matchers.equalTo(Request.GET)
106 );
107 MatcherAssert.assertThat(
108 container.take().method(),
109 Matchers.equalTo(Request.GET)
110 );
111 }
112
113 }