/
HttpOperationsTest.java
125 lines (107 loc) · 4.77 KB
/
HttpOperationsTest.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright (c) 2011-Present Pivotal Software Inc, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty.http;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.http.DefaultHttpContent;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.DefaultLastHttpContent;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.handler.codec.json.JsonObjectDecoder;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import reactor.netty.Connection;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
/**
* @author Stephane Maldini
*/
public class HttpOperationsTest {
@Test
public void httpAndJsonDecoders() {
EmbeddedChannel channel = new EmbeddedChannel();
Connection testContext = () -> channel;
ChannelHandler handler = new JsonObjectDecoder(true);
testContext.addHandlerLast("foo", handler);
HttpOperations.autoAddHttpExtractor(testContext, "foo", handler);
String json1 = "[{\"some\": 1} , {\"valu";
String json2 = "e\": true, \"test\": 1}]";
Object[] content = new Object[3];
content[0] = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
content[1] =
new DefaultHttpContent(Unpooled.copiedBuffer(json1, CharsetUtil.UTF_8));
content[2] = new DefaultLastHttpContent(Unpooled.copiedBuffer(json2,
CharsetUtil.UTF_8));
channel.writeInbound(content);
Object t = channel.readInbound();
assertThat(t, instanceOf(HttpResponse.class));
assertThat(t, not(instanceOf(HttpContent.class)));
t = channel.readInbound();
assertThat(t, instanceOf(ByteBuf.class));
ByteBuf b = (ByteBuf) t;
assertThat(b.readCharSequence(b.readableBytes(), CharsetUtil.UTF_8),
is("{\"some\": 1}"));
b.release();
t = channel.readInbound();
assertThat(t, instanceOf(ByteBuf.class));
b = (ByteBuf) t;
assertThat(b.readCharSequence(b.readableBytes(), CharsetUtil.UTF_8),
is("{\"value\": true, \"test\": 1}"));
b.release();
t = channel.readInbound();
assertThat(t, is(LastHttpContent.EMPTY_LAST_CONTENT));
((LastHttpContent) t).release();
t = channel.readInbound();
assertThat(t, nullValue());
}
@Test
public void testIssue948() {
assertEquals("", HttpOperations.resolvePath("http://localhost:8080"));
assertEquals("", HttpOperations.resolvePath("http://localhost:8080/"));
assertEquals("", HttpOperations.resolvePath("http://localhost:8080//"));
assertEquals("/", HttpOperations.resolvePath("http://localhost:8080///"));
assertEquals("a", HttpOperations.resolvePath("http://localhost:8080/a"));
assertEquals("a", HttpOperations.resolvePath("http://localhost:8080/a/"));
assertEquals("", HttpOperations.resolvePath("http://localhost:8080/?b"));
assertEquals("a", HttpOperations.resolvePath("http://localhost:8080/a?b"));
assertEquals("", HttpOperations.resolvePath("http://localhost:8080/#b"));
assertEquals("a", HttpOperations.resolvePath("http://localhost:8080/a#b"));
assertEquals("a", HttpOperations.resolvePath("http://localhost:8080/a?b#c"));
assertEquals("a b", HttpOperations.resolvePath("http://localhost:8080/a%20b"));
assertEquals("", HttpOperations.resolvePath(""));
assertEquals("", HttpOperations.resolvePath("/"));
assertEquals("", HttpOperations.resolvePath("//"));
assertEquals("/", HttpOperations.resolvePath("///"));
assertEquals("a", HttpOperations.resolvePath("/a"));
assertEquals("a", HttpOperations.resolvePath("/a/"));
assertEquals("", HttpOperations.resolvePath("/?b"));
assertEquals("a", HttpOperations.resolvePath("/a?b"));
assertEquals("", HttpOperations.resolvePath("/#b"));
assertEquals("a", HttpOperations.resolvePath("/a#b"));
assertEquals("a", HttpOperations.resolvePath("/a?b#c"));
assertEquals("a b", HttpOperations.resolvePath("/a%20b"));
}
}