| | | 1 | | using System; |
| | | 2 | | using System.Threading.Tasks; |
| | | 3 | | using JetBrains.Annotations; |
| | | 4 | | using WireMock.Matchers.Request; |
| | | 5 | | using WireMock.ResponseProviders; |
| | | 6 | | |
| | | 7 | | namespace WireMock |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// The Mapping. |
| | | 11 | | /// </summary> |
| | | 12 | | public class Mapping : IMapping |
| | | 13 | | { |
| | | 14 | | /// <inheritdoc cref="IMapping.Guid" /> |
| | 541 | 15 | | public Guid Guid { get; } |
| | | 16 | | |
| | | 17 | | /// <inheritdoc cref="IMapping.Title" /> |
| | 42 | 18 | | public string Title { get; } |
| | | 19 | | |
| | | 20 | | /// <inheritdoc cref="IMapping.Path" /> |
| | 250 | 21 | | public string Path { get; set; } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc cref="IMapping.Priority" /> |
| | 118 | 24 | | public int Priority { get; } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc cref="IMapping.Scenario" /> |
| | | 27 | | [CanBeNull] |
| | 1016 | 28 | | public string Scenario { get; } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc cref="IMapping.ExecutionConditionState" /> |
| | | 31 | | [CanBeNull] |
| | 37 | 32 | | public string ExecutionConditionState { get; } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc cref="IMapping.NextState" /> |
| | | 35 | | [CanBeNull] |
| | 29 | 36 | | public string NextState { get; } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc cref="IMapping.RequestMatcher" /> |
| | 284 | 39 | | public IRequestMatcher RequestMatcher { get; } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc cref="IMapping.Provider" /> |
| | 323 | 42 | | public IResponseProvider Provider { get; } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc cref="IMapping.IsStartState" /> |
| | 9 | 45 | | public bool IsStartState => Scenario == null || Scenario != null && NextState != null && ExecutionConditionState |
| | | 46 | | |
| | | 47 | | /// <inheritdoc cref="IMapping.IsAdminInterface" /> |
| | 110 | 48 | | public bool IsAdminInterface => Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Initializes a new instance of the <see cref="Mapping"/> class. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="guid">The unique identifier.</param> |
| | | 54 | | /// <param name="title">The unique title (can be null).</param> |
| | | 55 | | /// <param name="path">The full file path from this mapping title (can be null).</param> |
| | | 56 | | /// <param name="requestMatcher">The request matcher.</param> |
| | | 57 | | /// <param name="provider">The provider.</param> |
| | | 58 | | /// <param name="priority">The priority for this mapping.</param> |
| | | 59 | | /// <param name="scenario">The scenario. [Optional]</param> |
| | | 60 | | /// <param name="executionConditionState">State in which the current mapping can occur. [Optional]</param> |
| | | 61 | | /// <param name="nextState">The next state which will occur after the current mapping execution. [Optional]</par |
| | 250 | 62 | | public Mapping(Guid guid, [CanBeNull] string title, [CanBeNull] string path, IRequestMatcher requestMatcher, IRe |
| | 250 | 63 | | { |
| | 250 | 64 | | Guid = guid; |
| | 250 | 65 | | Title = title; |
| | 250 | 66 | | Path = path; |
| | 250 | 67 | | RequestMatcher = requestMatcher; |
| | 250 | 68 | | Provider = provider; |
| | 250 | 69 | | Priority = priority; |
| | 250 | 70 | | Scenario = scenario; |
| | 250 | 71 | | ExecutionConditionState = executionConditionState; |
| | 250 | 72 | | NextState = nextState; |
| | 250 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc cref="IMapping.ResponseToAsync" /> |
| | | 76 | | public async Task<ResponseMessage> ResponseToAsync(RequestMessage requestMessage) |
| | 35 | 77 | | { |
| | 35 | 78 | | return await Provider.ProvideResponseAsync(requestMessage); |
| | 35 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <inheritdoc cref="IMapping.GetRequestMatchResult" /> |
| | | 82 | | public RequestMatchResult GetRequestMatchResult(RequestMessage requestMessage, [CanBeNull] string nextState) |
| | 279 | 83 | | { |
| | 279 | 84 | | var result = new RequestMatchResult(); |
| | | 85 | | |
| | 279 | 86 | | RequestMatcher.GetMatchingScore(requestMessage, result); |
| | | 87 | | |
| | | 88 | | // Only check state if Scenario is defined |
| | 279 | 89 | | if (Scenario != null) |
| | 30 | 90 | | { |
| | 30 | 91 | | var matcher = new RequestMessageScenarioAndStateMatcher(nextState, ExecutionConditionState); |
| | 30 | 92 | | matcher.GetMatchingScore(requestMessage, result); |
| | | 93 | | //// If ExecutionConditionState is null, this means that request is the start from a scenario. So just r |
| | | 94 | | //if (ExecutionConditionState != null) |
| | | 95 | | //{ |
| | | 96 | | // // ExecutionConditionState is not null, so get score for matching with the nextState. |
| | | 97 | | // var matcher = new RequestMessageScenarioAndStateMatcher(nextState, ExecutionConditionState); |
| | | 98 | | // matcher.GetMatchingScore(requestMessage, result); |
| | | 99 | | //} |
| | 30 | 100 | | } |
| | | 101 | | |
| | 279 | 102 | | return result; |
| | 279 | 103 | | } |
| | | 104 | | } |
| | | 105 | | } |