// This source file is based on mock4net by Alexandre Victoor which is licensed under the Apache 2.0 License.
// For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root.
using System;
using WireMock.Matchers.Request;
using WireMock.ResponseProviders;
using WireMock.Settings;
namespace WireMock.Server
{
///
/// The respond with a provider.
///
internal class RespondWithAProvider : IRespondWithAProvider
{
private int _priority;
private string _title;
private string _path;
private string _executionConditionState;
private string _nextState;
private string _scenario;
private readonly RegistrationCallback _registrationCallback;
private readonly IRequestMatcher _requestMatcher;
private readonly IWireMockServerSettings _settings;
private readonly bool _saveToFile;
public Guid Guid { get; private set; } = Guid.NewGuid();
///
/// Initializes a new instance of the class.
///
/// The registration callback.
/// The request matcher.
/// The WireMockServerSettings.
/// Optional boolean to indicate if this mapping should be saved as static mapping file.
public RespondWithAProvider(RegistrationCallback registrationCallback, IRequestMatcher requestMatcher, IWireMockServerSettings settings, bool saveToFile = false)
{
_registrationCallback = registrationCallback;
_requestMatcher = requestMatcher;
_settings = settings;
_saveToFile = saveToFile;
}
///
/// The respond with.
///
/// The provider.
public void RespondWith(IResponseProvider provider)
{
_registrationCallback(new Mapping(Guid, _title, _path, _settings, _requestMatcher, provider, _priority, _scenario, _executionConditionState, _nextState), _saveToFile);
}
///
public IRespondWithAProvider WithGuid(string guid)
{
return WithGuid(Guid.Parse(guid));
}
///
public IRespondWithAProvider WithGuid(Guid guid)
{
Guid = guid;
return this;
}
///
public IRespondWithAProvider WithTitle(string title)
{
_title = title;
return this;
}
///
public IRespondWithAProvider WithPath(string path)
{
_path = path;
return this;
}
///
public IRespondWithAProvider AtPriority(int priority)
{
_priority = priority;
return this;
}
///
public IRespondWithAProvider InScenario(string scenario)
{
_scenario = scenario;
return this;
}
///
public IRespondWithAProvider InScenario(int scenario)
{
return InScenario(scenario.ToString());
}
///
public IRespondWithAProvider WhenStateIs(string state)
{
if (string.IsNullOrEmpty(_scenario))
{
throw new NotSupportedException("Unable to set state condition when no scenario is defined.");
}
_executionConditionState = state;
return this;
}
///
public IRespondWithAProvider WhenStateIs(int state)
{
return WhenStateIs(state.ToString());
}
///
public IRespondWithAProvider WillSetStateTo(string state)
{
if (string.IsNullOrEmpty(_scenario))
{
throw new NotSupportedException("Unable to set next state when no scenario is defined.");
}
_nextState = state;
return this;
}
///
public IRespondWithAProvider WillSetStateTo(int state)
{
return WillSetStateTo(state.ToString());
}
}
}