/
NettyAsyncResponseTest.java
90 lines (74 loc) · 3.54 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
90
/*
* 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 static org.testng.Assert.*;
import org.asynchttpclient.FluentCaseInsensitiveStringsMap;
import org.asynchttpclient.HttpResponseHeaders;
import org.asynchttpclient.cookie.Cookie;
import org.testng.annotations.Test;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
/**
* @author Benjamin Hanzelmann
*/
public class NettyAsyncResponseTest {
@Test(groups = "standalone")
public void testCookieParseExpires() {
// e.g. "Sun, 06-Feb-2012 03:45: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));
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null, null), new HttpResponseHeaders() {
@Override
public FluentCaseInsensitiveStringsMap getHeaders() {
return new FluentCaseInsensitiveStringsMap().add("Set-Cookie", cookieDef);
}
}, null);
List<Cookie> cookies = response.getCookies();
assertEquals(cookies.size(), 1);
Cookie cookie = cookies.get(0);
assertTrue(cookie.getMaxAge() >= 58 && cookie.getMaxAge() <= 60);
}
@Test(groups = "standalone")
public void testCookieParseMaxAge() {
final String cookieDef = "efmembercheck=true; max-age=60; path=/; domain=.eclipse.org";
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null, null), new HttpResponseHeaders() {
@Override
public FluentCaseInsensitiveStringsMap getHeaders() {
return new FluentCaseInsensitiveStringsMap().add("Set-Cookie", cookieDef);
}
}, null);
List<Cookie> cookies = response.getCookies();
assertEquals(cookies.size(), 1);
Cookie cookie = cookies.get(0);
assertEquals(cookie.getMaxAge(), 60);
}
@Test(groups = "standalone")
public void testCookieParseWeirdExpiresValue() {
final String cookieDef = "efmembercheck=true; expires=60; path=/; domain=.eclipse.org";
NettyResponse response = new NettyResponse(new NettyResponseStatus(null, null, null, null), new HttpResponseHeaders() {
@Override
public FluentCaseInsensitiveStringsMap getHeaders() {
return new FluentCaseInsensitiveStringsMap().add("Set-Cookie", cookieDef);
}
}, null);
List<Cookie> cookies = response.getCookies();
assertEquals(cookies.size(), 1);
Cookie cookie = cookies.get(0);
assertEquals(cookie.getMaxAge(), Long.MIN_VALUE);
}
}