content: /*
   * Copyright (C) 2016 Appflate.io
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package io.appflate.restmock.utils;
  
  import java.io.UnsupportedEncodingException;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.List;
  import java.util.Locale;
  
  import io.appflate.restmock.RESTMockServer;
  import okhttp3.mockwebserver.RecordedRequest;
  
  public final class RequestMatchers {
  
      private RequestMatchers() {
          throw new UnsupportedOperationException();
      }
  
      public static RequestMatcher pathContains(final String urlPart) {
          return new RequestMatcher("path contains: " + urlPart) {
  
              @Override
              protected boolean matchesSafely(RecordedRequest item) {
                  return item.getPath().toLowerCase(Locale.US).contains(urlPart.toLowerCase(Locale.US));
              }
          };
      }
  
      public static RequestMatcher pathDoesNotContain(final String urlPart) {
          return new RequestMatcher("path does not contain: " + urlPart) {
  
              @Override
              protected boolean matchesSafely(RecordedRequest item) {
                  return !item.getPath().toLowerCase(Locale.US).contains(urlPart.toLowerCase(Locale.US));
              }
          };
      }
  
      public static RequestMatcher pathMatchesRegex(final String pattern) {
          return new RequestMatcher("path matches with regex:" + pattern) {
  
              @Override
              protected boolean matchesSafely(RecordedRequest item) {
                  return item.getPath().toLowerCase(Locale.US).matches(pattern);
              }
          };
      }
  
      public static RequestMatcher pathEndsWith(final String urlPart) {
          return new RequestMatcher("path ends with: " + urlPart) {
  
              @Override
              protected boolean matchesSafely(RecordedRequest item) {
                  String urlPartWithoutEndingSlash = urlPart.replaceAll("/$", "");
                  String itemPathWithoutEndingSlash = item.getPath().replaceAll("/$", "");
                  return itemPathWithoutEndingSlash.toLowerCase(Locale.US).endsWith(urlPartWithoutEndingSlash.toLowerCase(Locale.US));
              }
          };
      }
  
      public static RequestMatcher pathEndsWithIgnoringQueryParams(final String endOfUrlPath) {
          return new RequestMatcher("path ends with: ${endOfUrlPath}") {
  
              protected boolean matchesSafely(RecordedRequest item) {
                  String endOfPathSanitized = sanitizePath(endOfUrlPath);
                  String recordedPathSanitized = sanitizePath(item.getPath());
                  return recordedPathSanitized.endsWith(endOfPathSanitized);
              }
          };
      }
  
      private static String sanitizePath(String path) {
          try {
              return new URL("http", "localhost", path).getPath().replaceAll("/$", "");
          } catch (MalformedURLException e) {
              throw new RuntimeException(e);
          }
      }
  
      /**
       * Checks whether matched request's path starts with given string.
       * path is a part of the url after the server's url. Example:
       * {@code pathStartsWith("login")} would match {@code https://localhost:4583/login}
       *
       * @param urlPart desired beginning of the path we want to match with
       * @return A new {@link RequestMatcher} object that will match {@link RecordedRequest} if it's
       * path starts with given urlPart
       */
      public static RequestMatcher pathStartsWith(final String urlPart) {
          return new RequestMatcher("path starts with: " + urlPart) {
  
              @Override
              protected boolean matchesSafely(RecordedRequest item) {
                  return item.getPath().toLowerCase(Locale.US).startsWith(urlPart.toLowerCase(Locale.US));
              }
          };
      }
  
      /**
       * Checks whether matched request contains non-null headers with given names
       *
       * @param headerNames names of headers to match
       * @return A new {@link RequestMatcher} object that will match {@link RecordedRequest} if it
       * contains specified header names
       */
      public static RequestMatcher hasHeaderNames(final String... headerNames) {
          return new RequestMatcher("has headers: " + Arrays.toString(headerNames)) {
  
              @Override
              protected boolean matchesSafely(RecordedRequest item) {
                  if (headerNames == null) {
                      throw new IllegalArgumentException("You did not specify any header names");
                  }
                  if (headerNames.length > 0 && item.getHeaders() == null) {
                      return false;
                  }
                  for (String header : headerNames) {
                      if (item.getHeader(header) == null) {
                          return false;
                      }
                  }
                  return true;
              }
          };
      }
  
      /**
       * Creates a {@link RequestMatcher} object for determining if a {@link RecordedRequest} contains
       * any query parameters.
       *
       * @return A new {@link RequestMatcher} object that will match a {@link RecordedRequest} if it
       * contains query parameters in its path.
       */
      public static RequestMatcher hasQueryParameters() {
          return new RequestMatcher("matched query parameters") {
  
              @Override
              protected boolean matchesSafely(RecordedRequest item) {
                  try {
                      URL mockUrl = new URL("http", "localhost", item.getPath());
                      List<QueryParam> queryParams = RestMockUtils.splitQuery(mockUrl);
                      return queryParams.size() > 0;
                  } catch (MalformedURLException e) {
                      return false;
                  } catch (UnsupportedEncodingException e) {
                      return false;
                  }
              }
          };
      }
  
      /**
       * Creates a {@link RequestMatcher} object for determining if a {@link RecordedRequest} contains
       * a specific set of query parameters names.
       *
       * @param expectedParamsNames A set of {@link String} objects to be matched.
       * @return A new {@link RequestMatcher} object that will match a {@link RecordedRequest} if it
       * contains the specified query parameters names in its path.
       */
      public static RequestMatcher hasQueryParameterNames(final String... expectedParamsNames) {
          return new RequestMatcher("matched query parameters names") {
  
              @Override
              protected boolean matchesSafely(RecordedRequest item) {
                  try {
                      URL mockUrl = new URL("http", "localhost", item.getPath());
                      List<QueryParam> actualParams = RestMockUtils.splitQuery(mockUrl);
                      if (actualParams.size() == 0 || expectedParamsNames.length == 0) {
                          return false;
                      }
  
                      List<String> expectedParamNamesList = varArgToList(expectedParamsNames);
  
                      int matchedParams = 0;
                      for (QueryParam actualParam : actualParams) {
                          if (expectedParamNamesList.contains(actualParam.getKey())) {
                              matchedParams++;
                          }
                      }
  
                      return matchedParams == expectedParamsNames.length;
  
                  } catch (MalformedURLException e) {
                      RESTMockServer.getLogger().error("URL appears to be malformed with path: " + item.getPath());
                      return false;
                  } catch (UnsupportedEncodingException e) {
                      return false;
                  }
              }
          };
      }
  
      /**
       * Creates a {@link RequestMatcher} object for determining if a {@link RecordedRequest} contains
       * a specific set of query parameters.
       *
       * @param expectedParams A set of {@link QueryParam} objects to be matched.
       * @return A new {@link RequestMatcher} object that will match a {@link RecordedRequest} if it
       * contains the specified query parameters in its path. Note that this
       * {@link RequestMatcher} only matches the exact specification of query parameters. That
       * is, if any of the key/value pairs don't match, or if the number of expected parameters
       * doesn't match the number of actual parameters, this {@link RequestMatcher} will
       * return false.
       */
      public static RequestMatcher hasExactQueryParameters(final QueryParam... expectedParams) {
          return new RequestMatcher("matched query parameters") {
  
              @Override
              protected boolean matchesSafely(RecordedRequest item) {
                  try {
                      URL mockUrl = new URL("http", "localhost", item.getPath());
                      List<QueryParam> actualParams = RestMockUtils.splitQuery(mockUrl);
                      if (actualParams.size() == 0 || actualParams.size() != expectedParams.length) {
                          return false;
                      }
  
                      for (QueryParam param : expectedParams) {
                          if (!actualParams.contains(param)) {
                              return false;
                          }
                      }
                      return true;
                  } catch (MalformedURLException e) {
                      RESTMockServer.getLogger().error("URL appears to be malformed with path: " + item.getPath());
                      return false;
                  } catch (UnsupportedEncodingException e) {
                      return false;
                  }
              }
          };
      }
  
      public static RequestMatcher httpMethodIs(final String method) {
          return new RequestMatcher("HTTP method is: " + method) {
  
              @Override
              protected boolean matchesSafely(final RecordedRequest item) {
                  return item.getMethod().equalsIgnoreCase(method);
              }
          };
      }
  
      public static RequestMatcher isGET() {
          return httpMethodIs("GET");
      }
  
      public static RequestMatcher isPOST() {
          return httpMethodIs("POST");
      }
  
      public static RequestMatcher isPATCH() {
          return httpMethodIs("PATCH");
      }
  
      public static RequestMatcher isDELETE() {
          return httpMethodIs("DELETE");
      }
  
      public static RequestMatcher isPUT() {
          return httpMethodIs("PUT");
      }
  
      public static RequestMatcher isHEAD() {
          return httpMethodIs("HEAD");
      }
  
      private static List<String> varArgToList(String... args) {
          List<String> strings = new ArrayList<>(args.length);
          Collections.addAll(strings, args);
          return strings;
      }
  }
  