View Javadoc
1   /*
2    * Copyright (C) 2018, Konrad Windszus <konrad_w@gmx.de> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
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  	 * The default {@link HttpCookie#equals(Object)} is not good enough for
39  	 * testing purposes. Also the {@link HttpCookie#toString()} only emits some
40  	 * of the cookie attributes. For testing a dedicated matcher is needed which
41  	 * takes into account all attributes.
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  			// the equals method in HttpCookie is not specific enough, we want
70  			// to consider all attributes!
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 }