Iterable<WhenHttpRequest> findWhensMatchingRequest( ImmutableMap<String, RestxSpec> allSpecs, RestxRequest restxRequest) { Collection<WhenHttpRequest> matchingRequestsSpecs = Lists.newArrayList(); for (Map.Entry<String, RestxSpec> spec : allSpecs.entrySet()) { for (When when : spec.getValue().getWhens()) { if (when instanceof WhenHttpRequest) { WhenHttpRequest request = (WhenHttpRequest) when; String requestPath = request.getPath(); if (!requestPath.startsWith("/")) { requestPath = "/" + requestPath; } StdRequest stdRequest = StdRequest.builder() .setBaseUri("http://restx.io") // baseUri is required but we won't use it .setHttpMethod(request.getMethod()) .setFullPath(requestPath) .build(); if (restxRequest.getHttpMethod().equals(stdRequest.getHttpMethod()) && restxRequest.getRestxPath().equals(stdRequest.getRestxPath())) { MapDifference<String, ImmutableList<String>> difference = Maps.difference(restxRequest.getQueryParams(), stdRequest.getQueryParams()); if (difference.entriesOnlyOnRight().isEmpty() && difference.entriesDiffering().isEmpty()) { matchingRequestsSpecs.add(request); break; } } } } } return matchingRequestsSpecs; }
protected Map<String, RestxSpec> buildSpecsMap(boolean searchInSources) { Map<String, RestxSpec> specsMap = Maps.newLinkedHashMap(); Map<String, URL> specs = MoreResources.findResources("", Pattern.compile(".*\\.spec\\.yaml"), searchInSources); for (final Map.Entry<String, URL> spec : specs.entrySet()) { try { specsMap.put( spec.getKey(), specLoader.load( spec.getKey(), new CharSource() { @Override public Reader openStream() throws IOException { return new InputStreamReader(spec.getValue().openStream(), Charsets.UTF_8); } })); } catch (Exception e) { logger.warn("exception while loading restx spec " + spec + ": " + e, e); } } return specsMap; }