| | | 1 | | using System; |
| | | 2 | | using WireMock.Matchers.Request; |
| | | 3 | | using WireMock.ResponseProviders; |
| | | 4 | | |
| | | 5 | | namespace WireMock.Server |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// The respond with a provider. |
| | | 9 | | /// </summary> |
| | | 10 | | internal class RespondWithAProvider : IRespondWithAProvider |
| | | 11 | | { |
| | | 12 | | private int _priority; |
| | | 13 | | private string _title; |
| | | 14 | | private string _path; |
| | | 15 | | private string _executionConditionState; |
| | | 16 | | private string _nextState; |
| | | 17 | | private string _scenario; |
| | | 18 | | private readonly RegistrationCallback _registrationCallback; |
| | | 19 | | private readonly IRequestMatcher _requestMatcher; |
| | | 20 | | |
| | 522 | 21 | | public Guid Guid { get; private set; } = Guid.NewGuid(); |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="RespondWithAProvider"/> class. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="registrationCallback">The registration callback.</param> |
| | | 27 | | /// <param name="requestMatcher">The request matcher.</param> |
| | 249 | 28 | | public RespondWithAProvider(RegistrationCallback registrationCallback, IRequestMatcher requestMatcher) |
| | 249 | 29 | | { |
| | 249 | 30 | | _registrationCallback = registrationCallback; |
| | 249 | 31 | | _requestMatcher = requestMatcher; |
| | 249 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// The respond with. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="provider">The provider.</param> |
| | | 38 | | public void RespondWith(IResponseProvider provider) |
| | 249 | 39 | | { |
| | 249 | 40 | | _registrationCallback(new Mapping(Guid, _title, _path, _requestMatcher, provider, _priority, _scenario, _exe |
| | 249 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <see cref="IRespondWithAProvider.WithGuid(string)"/> |
| | | 44 | | public IRespondWithAProvider WithGuid(string guid) |
| | 2 | 45 | | { |
| | 2 | 46 | | return WithGuid(Guid.Parse(guid)); |
| | 2 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <see cref="IRespondWithAProvider.WithGuid(Guid)"/> |
| | | 50 | | public IRespondWithAProvider WithGuid(Guid guid) |
| | 14 | 51 | | { |
| | 14 | 52 | | Guid = guid; |
| | | 53 | | |
| | 14 | 54 | | return this; |
| | 14 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <see cref="IRespondWithAProvider.WithTitle"/> |
| | | 58 | | public IRespondWithAProvider WithTitle(string title) |
| | 4 | 59 | | { |
| | 4 | 60 | | _title = title; |
| | | 61 | | |
| | 4 | 62 | | return this; |
| | 4 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <see cref="IRespondWithAProvider.WithPath"/> |
| | | 66 | | public IRespondWithAProvider WithPath(string path) |
| | 10 | 67 | | { |
| | 10 | 68 | | _path = path; |
| | | 69 | | |
| | 10 | 70 | | return this; |
| | 10 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <see cref="IRespondWithAProvider.AtPriority"/> |
| | | 74 | | public IRespondWithAProvider AtPriority(int priority) |
| | 221 | 75 | | { |
| | 221 | 76 | | _priority = priority; |
| | | 77 | | |
| | 221 | 78 | | return this; |
| | 221 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <see cref="IRespondWithAProvider.InScenario(string)"/> |
| | | 82 | | public IRespondWithAProvider InScenario(string scenario) |
| | 10 | 83 | | { |
| | 10 | 84 | | _scenario = scenario; |
| | | 85 | | |
| | 10 | 86 | | return this; |
| | 10 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <see cref="IRespondWithAProvider.WhenStateIs"/> |
| | | 90 | | public IRespondWithAProvider WhenStateIs(string state) |
| | 6 | 91 | | { |
| | 6 | 92 | | if (string.IsNullOrEmpty(_scenario)) |
| | 0 | 93 | | { |
| | 0 | 94 | | throw new NotSupportedException("Unable to set state condition when no scenario is defined."); |
| | | 95 | | } |
| | | 96 | | |
| | 6 | 97 | | _executionConditionState = state; |
| | | 98 | | |
| | 6 | 99 | | return this; |
| | 6 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <see cref="IRespondWithAProvider.WillSetStateTo"/> |
| | | 103 | | public IRespondWithAProvider WillSetStateTo(string state) |
| | 5 | 104 | | { |
| | 5 | 105 | | if (string.IsNullOrEmpty(_scenario)) |
| | 0 | 106 | | { |
| | 0 | 107 | | throw new NotSupportedException("Unable to set next state when no scenario is defined."); |
| | | 108 | | } |
| | | 109 | | |
| | 5 | 110 | | _nextState = state; |
| | | 111 | | |
| | 5 | 112 | | return this; |
| | 5 | 113 | | } |
| | | 114 | | } |
| | | 115 | | } |