|
| 1 | +package com.auth0.client; |
| 2 | + |
| 3 | +import com.auth0.net.client.HttpMethod; |
| 4 | +import okhttp3.HttpUrl; |
| 5 | +import okhttp3.mockwebserver.RecordedRequest; |
| 6 | +import org.hamcrest.Description; |
| 7 | +import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 8 | + |
| 9 | +public class RecordedRequestMatcher extends TypeSafeDiagnosingMatcher<RecordedRequest> { |
| 10 | + |
| 11 | + private static final int METHOD_PATH = 0; |
| 12 | + private static final int HEADER = 1; |
| 13 | + private static final int QUERY_PARAMETER = 2; |
| 14 | + private static final int QUERY_PARAMETER_PRESENCE = 3; |
| 15 | + |
| 16 | + private final int checkingOption; |
| 17 | + private final String first; |
| 18 | + private final String second; |
| 19 | + |
| 20 | + private RecordedRequestMatcher(String first, String second, int checkingOption) { |
| 21 | + this.checkingOption = checkingOption; |
| 22 | + this.first = first; |
| 23 | + this.second = second; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + protected boolean matchesSafely(RecordedRequest item, Description mismatchDescription) { |
| 28 | + if (item == null) { |
| 29 | + mismatchDescription.appendText("was null"); |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + switch (checkingOption) { |
| 34 | + default: |
| 35 | + case METHOD_PATH: |
| 36 | + return matchesMethodAndPath(item, mismatchDescription); |
| 37 | + case HEADER: |
| 38 | + return matchesHeader(item, mismatchDescription); |
| 39 | + case QUERY_PARAMETER: |
| 40 | + return matchesQueryParameter(item, mismatchDescription); |
| 41 | + case QUERY_PARAMETER_PRESENCE: |
| 42 | + return hasQueryParameter(item, mismatchDescription); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private boolean matchesMethodAndPath(RecordedRequest item, Description mismatchDescription) { |
| 47 | + if (!item.getMethod().equalsIgnoreCase(first)) { |
| 48 | + mismatchDescription.appendText("method was ").appendValue(item.getMethod()); |
| 49 | + return false; |
| 50 | + } |
| 51 | + String path = item.getPath(); |
| 52 | + boolean hasQuery = path.indexOf("?") > 0; |
| 53 | + if (hasQuery) { |
| 54 | + path = path.substring(0, path.indexOf("?")); |
| 55 | + } |
| 56 | + if (!path.equals(second)) { |
| 57 | + mismatchDescription.appendText("path was ").appendValue(path); |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + private boolean matchesHeader(RecordedRequest item, Description mismatchDescription) { |
| 65 | + String value = item.getHeader(first); |
| 66 | + if (value != null && !value.equals(second) || value == null && second != null) { |
| 67 | + mismatchDescription.appendText(first).appendText(" header was ").appendValue(value); |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + private boolean matchesQueryParameter(RecordedRequest item, Description mismatchDescription) { |
| 75 | + HttpUrl requestUrl = item.getRequestUrl(); |
| 76 | + if (requestUrl.querySize() == 0) { |
| 77 | + mismatchDescription.appendText(" query was empty"); |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + String queryParamValue = requestUrl.queryParameter(first); |
| 82 | + if (second.equals(queryParamValue)) { |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + mismatchDescription.appendValueList( |
| 87 | + "Query parameters were {", ", ", "}.", requestUrl.query().split("&")); |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + private boolean hasQueryParameter(RecordedRequest item, Description mismatchDescription) { |
| 92 | + String path = item.getPath(); |
| 93 | + boolean hasQuery = path.indexOf("?") > 0; |
| 94 | + if (!hasQuery) { |
| 95 | + mismatchDescription.appendText(" query was empty"); |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + String query = path.substring(path.indexOf("?") + 1); |
| 100 | + String[] parameters = query.split("&"); |
| 101 | + for (String p : parameters) { |
| 102 | + if (p.startsWith(String.format("%s=", first))) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + } |
| 106 | + mismatchDescription.appendValueList("Query parameters were {", ", ", "}.", parameters); |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void describeTo(Description description) { |
| 112 | + switch (checkingOption) { |
| 113 | + default: |
| 114 | + case METHOD_PATH: |
| 115 | + description |
| 116 | + .appendText("A request with method ") |
| 117 | + .appendValue(first) |
| 118 | + .appendText(" and path ") |
| 119 | + .appendValue(second); |
| 120 | + break; |
| 121 | + case HEADER: |
| 122 | + description |
| 123 | + .appendText("A request containing header ") |
| 124 | + .appendValue(first) |
| 125 | + .appendText(" with value ") |
| 126 | + .appendValue(second); |
| 127 | + break; |
| 128 | + case QUERY_PARAMETER: |
| 129 | + description |
| 130 | + .appendText("A request containing query parameter ") |
| 131 | + .appendValue(first) |
| 132 | + .appendText(" with value ") |
| 133 | + .appendValue(second); |
| 134 | + break; |
| 135 | + case QUERY_PARAMETER_PRESENCE: |
| 136 | + description.appendText("A request containing query parameter ").appendValue(first); |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public static RecordedRequestMatcher hasMethodAndPath(HttpMethod method, String path) { |
| 142 | + return new RecordedRequestMatcher(method.toString(), path, METHOD_PATH); |
| 143 | + } |
| 144 | + |
| 145 | + public static RecordedRequestMatcher hasHeader(String name, String value) { |
| 146 | + return new RecordedRequestMatcher(name, value, HEADER); |
| 147 | + } |
| 148 | + |
| 149 | + public static RecordedRequestMatcher hasQueryParameter(String name, String value) { |
| 150 | + return new RecordedRequestMatcher(name, value, QUERY_PARAMETER); |
| 151 | + } |
| 152 | + |
| 153 | + public static RecordedRequestMatcher hasQueryParameter(String name) { |
| 154 | + return new RecordedRequestMatcher(name, null, QUERY_PARAMETER_PRESENCE); |
| 155 | + } |
| 156 | +} |
0 commit comments