public virtual int FindMatch(Request request, IList <RecordEntry> entries) { SortedDictionary <string, string[]> headers = new SortedDictionary <string, string[]>(StringComparer.OrdinalIgnoreCase); foreach (var header in request.Headers) { var gotHeader = request.Headers.TryGetValues(header.Name, out IEnumerable <string> values); Debug.Assert(gotHeader); headers[header.Name] = values.ToArray(); } _sanitizer.SanitizeHeaders(headers); string uri = _sanitizer.SanitizeUri(request.UriBuilder.ToString()); for (int i = 0; i < entries.Count; i++) { RecordEntry entry = entries[i]; if (entry.RequestUri == uri && entry.RequestMethod == request.Method && CompareHeaderDictionaries(headers, entry.RequestHeaders)) { return(i); } } return(-1); }
public virtual RecordEntry FindMatch(Request request, IList <RecordEntry> entries) { SortedDictionary <string, string[]> headers = new SortedDictionary <string, string[]>(StringComparer.OrdinalIgnoreCase); foreach (HttpHeader header in request.Headers) { var gotHeader = request.Headers.TryGetValues(header.Name, out IEnumerable <string> values); Debug.Assert(gotHeader); headers[header.Name] = values.ToArray(); } _sanitizer.SanitizeHeaders(headers); string uri = _sanitizer.SanitizeUri(request.Uri.ToString()); int bestScore = int.MaxValue; RecordEntry bestScoreEntry = null; foreach (RecordEntry entry in entries) { int score = 0; if (!AreUrisSame(entry.RequestUri, uri)) { score++; } if (entry.RequestMethod != request.Method) { score++; } score += CompareHeaderDictionaries(headers, entry.RequestHeaders, ExcludeHeaders); if (score == 0) { return(entry); } if (score < bestScore) { bestScoreEntry = entry; bestScore = score; } } throw new InvalidOperationException(GenerateException(request.Method, uri, headers, bestScoreEntry)); }
public virtual int FindMatch(Request request, IList <RecordEntry> entries, out string failureMessage) { SortedDictionary <string, string[]> headers = new SortedDictionary <string, string[]>(StringComparer.OrdinalIgnoreCase); foreach (var header in request.Headers) { var gotHeader = request.Headers.TryGetValues(header.Name, out IEnumerable <string> values); Debug.Assert(gotHeader); headers[header.Name] = values.ToArray(); } _sanitizer.SanitizeHeaders(headers); string uri = _sanitizer.SanitizeUri(request.UriBuilder.ToString()); int bestScore = int.MaxValue; var messagePrefix = failureMessage = $"Unable to find recorded request for {request.Method} {request.UriBuilder.ToString()}"; for (int i = 0; i < entries.Count; i++) { RecordEntry entry = entries[i]; if (entry.RequestUri == uri && entry.RequestMethod == request.Method) { int score = CompareHeaderDictionaries(headers, entry.RequestHeaders, out var diff); if (score == 0) { failureMessage = null; return(i); } else if (score < bestScore) { bestScore = score; failureMessage = $"{messagePrefix} (Best match: {diff})"; } } } if (bestScore == int.MaxValue && entries.Count == 1) { failureMessage = $"{messagePrefix} (Best match: {entries[0].RequestMethod} {entries[0].RequestUri})"; } return(-1); }