forked from AsyncHttpClient/async-http-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNettyAsyncResponseTest.java
89 lines (72 loc) · 4 KB
/
NettyAsyncResponseTest.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
/*
* Copyright (c) 2010-2012 Sonatype, Inc. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package org.asynchttpclient.netty;
import io.github.artsok.RepeatedIfExceptionsTest;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.cookie.Cookie;
import org.asynchttpclient.HttpResponseBodyPart;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import static io.netty.handler.codec.http.HttpHeaderNames.SET_COOKIE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class NettyAsyncResponseTest {
@RepeatedIfExceptionsTest(repeats = 5)
public void testCookieParseExpires() {
// e.g. "Tue, 27 Oct 2015 12:54:24 GMT";
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = new Date(System.currentTimeMillis() + 60000);
final String cookieDef = String.format("efmembercheck=true; expires=%s; path=/; domain=.eclipse.org", sdf.format(date));
HttpHeaders responseHeaders = new DefaultHttpHeaders().add(SET_COOKIE, cookieDef);
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null), responseHeaders, null);
List<Cookie> cookies = response.getCookies();
assertEquals(1, cookies.size());
Cookie cookie = cookies.get(0);
assertTrue(cookie.maxAge() >= 58 && cookie.maxAge() <= 60);
}
@RepeatedIfExceptionsTest(repeats = 5)
public void testCookieParseMaxAge() {
final String cookieDef = "efmembercheck=true; max-age=60; path=/; domain=.eclipse.org";
HttpHeaders responseHeaders = new DefaultHttpHeaders().add(SET_COOKIE, cookieDef);
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null), responseHeaders, null);
List<Cookie> cookies = response.getCookies();
assertEquals(1, cookies.size());
Cookie cookie = cookies.get(0);
assertEquals(60, cookie.maxAge());
}
@RepeatedIfExceptionsTest(repeats = 5)
public void testCookieParseWeirdExpiresValue() {
final String cookieDef = "efmembercheck=true; expires=60; path=/; domain=.eclipse.org";
HttpHeaders responseHeaders = new DefaultHttpHeaders().add(SET_COOKIE, cookieDef);
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null), responseHeaders, null);
List<Cookie> cookies = response.getCookies();
assertEquals(1, cookies.size());
Cookie cookie = cookies.get(0);
assertEquals(Long.MIN_VALUE, cookie.maxAge());
}
@RepeatedIfExceptionsTest(repeats = 5)
public void testGetResponseBodyAsByteBuffer() {
List<HttpResponseBodyPart> bodyParts = new LinkedList<>();
bodyParts.add(new LazyResponseBodyPart(Unpooled.wrappedBuffer("Hello ".getBytes()), false));
bodyParts.add(new LazyResponseBodyPart(Unpooled.wrappedBuffer("World".getBytes()), true));
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null), null, bodyParts);
ByteBuf body = response.getResponseBodyAsByteBuf();
assertEquals("Hello World", body.toString(StandardCharsets.UTF_8));
body.release();
}
}