1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.util.http;
11
12 import java.net.HttpCookie;
13 import java.util.LinkedList;
14 import java.util.List;
15
16 import org.hamcrest.Description;
17 import org.hamcrest.Matcher;
18 import org.hamcrest.TypeSafeMatcher;
19 import org.hamcrest.collection.IsIterableContainingInOrder;
20
21 public final class HttpCookiesMatcher {
22 public static Matcher<Iterable<? extends HttpCookie>> containsInOrder(
23 Iterable<HttpCookie> expectedCookies) {
24 return containsInOrder(expectedCookies, 0);
25 }
26
27 public static Matcher<Iterable<? extends HttpCookie>> containsInOrder(
28 Iterable<HttpCookie> expectedCookies, int allowedMaxAgeDelta) {
29 final List<Matcher<? super HttpCookie>> cookieMatchers = new LinkedList<>();
30 for (HttpCookie cookie : expectedCookies) {
31 cookieMatchers
32 .add(new HttpCookieMatcher(cookie, allowedMaxAgeDelta));
33 }
34 return new IsIterableContainingInOrder<>(cookieMatchers);
35 }
36
37
38
39
40
41
42
43 private static final class HttpCookieMatcher
44 extends TypeSafeMatcher<HttpCookie> {
45
46 private final HttpCookie cookie;
47
48 private final int allowedMaxAgeDelta;
49
50 public HttpCookieMatcher(HttpCookie cookie, int allowedMaxAgeDelta) {
51 this.cookie = cookie;
52 this.allowedMaxAgeDelta = allowedMaxAgeDelta;
53 }
54
55 @Override
56 public void describeTo(Description description) {
57 describeCookie(description, cookie);
58 }
59
60 @Override
61 protected void describeMismatchSafely(HttpCookie item,
62 Description mismatchDescription) {
63 mismatchDescription.appendText("was ");
64 describeCookie(mismatchDescription, item);
65 }
66
67 @Override
68 protected boolean matchesSafely(HttpCookie otherCookie) {
69
70
71 return (equals(cookie.getName(), otherCookie.getName())
72 && equals(cookie.getValue(), otherCookie.getValue())
73 && equals(cookie.getDomain(), otherCookie.getDomain())
74 && equals(cookie.getPath(), otherCookie.getPath())
75 && (cookie.getMaxAge() >= otherCookie.getMaxAge()
76 - allowedMaxAgeDelta)
77 && (cookie.getMaxAge() <= otherCookie.getMaxAge()
78 + allowedMaxAgeDelta)
79 && cookie.isHttpOnly() == otherCookie.isHttpOnly()
80 && cookie.getSecure() == otherCookie.getSecure()
81 && cookie.getVersion() == otherCookie.getVersion());
82 }
83
84 private static boolean equals(String value1, String value2) {
85 if (value1 == null && value2 == null) {
86 return true;
87 }
88 if (value1 == null || value2 == null) {
89 return false;
90 }
91 return value1.equals(value2);
92 }
93
94 @SuppressWarnings("boxing")
95 protected static void describeCookie(Description description,
96 HttpCookie cookie) {
97 description.appendText("HttpCookie[");
98 description.appendText("name: ").appendValue(cookie.getName())
99 .appendText(", ");
100 description.appendText("value: ").appendValue(cookie.getValue())
101 .appendText(", ");
102 description.appendText("domain: ").appendValue(cookie.getDomain())
103 .appendText(", ");
104 description.appendText("path: ").appendValue(cookie.getPath())
105 .appendText(", ");
106 description.appendText("maxAge: ").appendValue(cookie.getMaxAge())
107 .appendText(", ");
108 description.appendText("httpOnly: ")
109 .appendValue(cookie.isHttpOnly()).appendText(", ");
110 description.appendText("secure: ").appendValue(cookie.getSecure())
111 .appendText(", ");
112 description.appendText("version: ").appendValue(cookie.getVersion())
113 .appendText(", ");
114 description.appendText("]");
115 }
116 }
117 }