View Javadoc
1   /*
2    * Copyright (C) 2018, Konrad Windszus <konrad_w@gmx.de>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  	 * The default {@link HttpCookie#equals(Object)} is not good enough for
72  	 * testing purposes. Also the {@link HttpCookie#toString()} only emits some
73  	 * of the cookie attributes. For testing a dedicated matcher is needed which
74  	 * takes into account all attributes.
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 			// the equals method in HttpCookie is not specific enough, we want
103 			// to consider all attributes!
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 }