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 package org.eclipse.jgit.util.http;
44
45 import java.net.HttpCookie;
46 import java.util.LinkedList;
47 import java.util.List;
48
49 import org.hamcrest.Description;
50 import org.hamcrest.Matcher;
51 import org.hamcrest.TypeSafeMatcher;
52 import org.hamcrest.collection.IsIterableContainingInOrder;
53
54 public final class HttpCookiesMatcher {
55 public static Matcher<Iterable<? extends HttpCookie>> containsInOrder(
56 Iterable<HttpCookie> expectedCookies) {
57 return containsInOrder(expectedCookies, 0);
58 }
59
60 public static Matcher<Iterable<? extends HttpCookie>> containsInOrder(
61 Iterable<HttpCookie> expectedCookies, int allowedMaxAgeDelta) {
62 final List<Matcher<? super HttpCookie>> cookieMatchers = new LinkedList<>();
63 for (HttpCookie cookie : expectedCookies) {
64 cookieMatchers
65 .add(new HttpCookieMatcher(cookie, allowedMaxAgeDelta));
66 }
67 return new IsIterableContainingInOrder<>(cookieMatchers);
68 }
69
70
71
72
73
74
75
76 private static final class HttpCookieMatcher
77 extends TypeSafeMatcher<HttpCookie> {
78
79 private final HttpCookie cookie;
80
81 private final int allowedMaxAgeDelta;
82
83 public HttpCookieMatcher(HttpCookie cookie, int allowedMaxAgeDelta) {
84 this.cookie = cookie;
85 this.allowedMaxAgeDelta = allowedMaxAgeDelta;
86 }
87
88 @Override
89 public void describeTo(Description description) {
90 describeCookie(description, cookie);
91 }
92
93 @Override
94 protected void describeMismatchSafely(HttpCookie item,
95 Description mismatchDescription) {
96 mismatchDescription.appendText("was ");
97 describeCookie(mismatchDescription, item);
98 }
99
100 @Override
101 protected boolean matchesSafely(HttpCookie otherCookie) {
102
103
104 return (equals(cookie.getName(), otherCookie.getName())
105 && equals(cookie.getValue(), otherCookie.getValue())
106 && equals(cookie.getDomain(), otherCookie.getDomain())
107 && equals(cookie.getPath(), otherCookie.getPath())
108 && (cookie.getMaxAge() >= otherCookie.getMaxAge()
109 - allowedMaxAgeDelta)
110 && (cookie.getMaxAge() <= otherCookie.getMaxAge()
111 + allowedMaxAgeDelta)
112 && cookie.isHttpOnly() == otherCookie.isHttpOnly()
113 && cookie.getSecure() == otherCookie.getSecure()
114 && cookie.getVersion() == otherCookie.getVersion());
115 }
116
117 private static boolean equals(String value1, String value2) {
118 if (value1 == null && value2 == null) {
119 return true;
120 }
121 if (value1 == null || value2 == null) {
122 return false;
123 }
124 return value1.equals(value2);
125 }
126
127 @SuppressWarnings("boxing")
128 protected static void describeCookie(Description description,
129 HttpCookie cookie) {
130 description.appendText("HttpCookie[");
131 description.appendText("name: ").appendValue(cookie.getName())
132 .appendText(", ");
133 description.appendText("value: ").appendValue(cookie.getValue())
134 .appendText(", ");
135 description.appendText("domain: ").appendValue(cookie.getDomain())
136 .appendText(", ");
137 description.appendText("path: ").appendValue(cookie.getPath())
138 .appendText(", ");
139 description.appendText("maxAge: ").appendValue(cookie.getMaxAge())
140 .appendText(", ");
141 description.appendText("httpOnly: ")
142 .appendValue(cookie.isHttpOnly()).appendText(", ");
143 description.appendText("secure: ").appendValue(cookie.getSecure())
144 .appendText(", ");
145 description.appendText("version: ").appendValue(cookie.getVersion())
146 .appendText(", ");
147 description.appendText("]");
148 }
149 }
150 }