using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using WireMock.Models;
using WireMock.Types;
using WireMock.Util;
using WireMock.Validation;
namespace WireMock
{
///
/// The RequestMessage.
///
public class RequestMessage
{
///
/// Gets the Client IP Address.
///
public string ClientIP { get; }
///
/// Gets the url (relative).
///
public string Url { get; }
///
/// Gets the AbsoluteUrl.
///
public string AbsoluteUrl { get; }
///
/// Gets the DateTime.
///
public DateTime DateTime { get; set; }
///
/// Gets the path (relative).
///
public string Path { get; }
///
/// Gets the AbsolutePath.
///
public string AbsolutePath { get; }
///
/// Gets the path segments.
///
public string[] PathSegments { get; }
///
/// Gets the absolute path segments.
///
public string[] AbsolutePathSegments { get; }
///
/// Gets the method.
///
public string Method { get; }
///
/// Gets the headers.
///
public IDictionary> Headers { get; }
///
/// Gets the cookies.
///
public IDictionary Cookies { get; }
///
/// Gets the query.
///
public IDictionary> Query { get; }
///
/// Gets the raw query.
///
public string RawQuery { get; }
///
/// The body.
///
public BodyData BodyData { get; }
///
/// The original body as string. Convenience getter for Handlebars.
///
public string Body { get; }
///
/// The body (as JSON object). Convenience getter for Handlebars.
///
public object BodyAsJson { get; }
///
/// The body (as bytearray). Convenience getter for Handlebars.
///
public byte[] BodyAsBytes { get; }
///
/// The detected body type. Convenience getter for Handlebars.
///
public string DetectedBodyType { get; }
///
/// The detected body type from the Content-Type header. Convenience getter for Handlebars.
///
public string DetectedBodyTypeFromContentType { get; }
///
/// Gets the Host
///
public string Host { get; }
///
/// Gets the protocol
///
public string Protocol { get; }
///
/// Gets the port
///
public int Port { get; }
///
/// Gets the origin
///
public string Origin { get; }
///
/// Initializes a new instance of the class.
///
/// The original url details.
/// The HTTP method.
/// The client IP Address.
/// The BodyData.
/// The headers.
/// The cookies.
public RequestMessage([NotNull] UrlDetails urlDetails, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] BodyData bodyData = null, [CanBeNull] IDictionary headers = null, [CanBeNull] IDictionary cookies = null)
{
Check.NotNull(urlDetails, nameof(urlDetails));
Check.NotNull(method, nameof(method));
Check.NotNull(clientIP, nameof(clientIP));
AbsoluteUrl = urlDetails.AbsoluteUrl.ToString();
Url = urlDetails.Url.ToString();
Protocol = urlDetails.Url.Scheme;
Host = urlDetails.Url.Host;
Port = urlDetails.Url.Port;
Origin = $"{Protocol}://{Host}:{Port}";
AbsolutePath = WebUtility.UrlDecode(urlDetails.AbsoluteUrl.AbsolutePath);
Path = WebUtility.UrlDecode(urlDetails.Url.AbsolutePath);
PathSegments = Path.Split('/').Skip(1).ToArray();
AbsolutePathSegments = AbsolutePath.Split('/').Skip(1).ToArray();
Method = method;
ClientIP = clientIP;
BodyData = bodyData;
// Convenience getters for e.g. Handlebars
Body = BodyData?.BodyAsString;
BodyAsJson = BodyData?.BodyAsJson;
BodyAsBytes = BodyData?.BodyAsBytes;
DetectedBodyType = BodyData?.DetectedBodyType.ToString();
DetectedBodyTypeFromContentType = BodyData?.DetectedBodyTypeFromContentType.ToString();
Headers = headers?.ToDictionary(header => header.Key, header => new WireMockList(header.Value));
Cookies = cookies;
RawQuery = urlDetails.Url.Query;
Query = QueryStringParser.Parse(RawQuery);
}
///
/// Get a query parameter.
///
/// The key.
/// Defines if the key should be matched using case-ignore.
/// The query parameter.
public WireMockList GetParameter(string key, bool ignoreCase = false)
{
if (Query == null)
{
return null;
}
var query = !ignoreCase ? Query : new Dictionary>(Query, StringComparer.OrdinalIgnoreCase);
return query.ContainsKey(key) ? query[key] : null;
}
}
}