mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-18 07:13:46 +01:00
Add helpers for query params fluent MappingModelBuilder (#1425)
* Add helpers for query params * add example with query params * add fluent helpers for WithHeaders and WithCookies --------- Co-authored-by: Logan Dam <Logan.Dam@rabobank.com> Co-authored-by: Stef Heyenrath <Stef.Heyenrath@gmail.com>
This commit is contained in:
@@ -82,6 +82,51 @@ class Program
|
||||
)
|
||||
);
|
||||
|
||||
mappingBuilder.Given(m => m
|
||||
.WithRequest(req => req
|
||||
.WithPath("/testRequestWithQueryParams")
|
||||
.UsingGet()
|
||||
.WithParams(p => p.WithParam("param1", pb => pb.WithExactMatcher("value1")))
|
||||
).WithResponse(rsp => rsp
|
||||
.WithHeaders(h => h.Add("Content-Type", "application/json"))
|
||||
.WithStatusCode(200)
|
||||
.WithBodyAsJson(new
|
||||
{
|
||||
status = "ok"
|
||||
}, true)
|
||||
)
|
||||
);
|
||||
|
||||
mappingBuilder.Given(m => m
|
||||
.WithRequest(req => req
|
||||
.WithPath("/testRequestWithHeaders")
|
||||
.UsingGet()
|
||||
.WithHeaders(h => h.WithHeader("Accept", hb => hb.WithExactMatcher("application/json")))
|
||||
).WithResponse(rsp => rsp
|
||||
.WithHeaders(h => h.Add("Content-Type", "application/json"))
|
||||
.WithStatusCode(200)
|
||||
.WithBodyAsJson(new
|
||||
{
|
||||
status = "ok"
|
||||
}, true)
|
||||
)
|
||||
);
|
||||
|
||||
mappingBuilder.Given(m => m
|
||||
.WithRequest(req => req
|
||||
.WithPath("/testRequestWithCookie")
|
||||
.UsingGet()
|
||||
.WithCookies(c => c.WithCookie("cookie1", cb => cb.WithExactMatcher("cookievalue1")))
|
||||
).WithResponse(rsp => rsp
|
||||
.WithHeaders(h => h.Add("Content-Type", "application/json"))
|
||||
.WithStatusCode(200)
|
||||
.WithBodyAsJson(new
|
||||
{
|
||||
status = "ok"
|
||||
}, true)
|
||||
)
|
||||
);
|
||||
|
||||
var result = await mappingBuilder.BuildAndPostAsync().ConfigureAwait(false);
|
||||
Console.WriteLine($"result = {JsonConvert.SerializeObject(result)}");
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
namespace WireMock.Admin.Mappings;
|
||||
|
||||
public partial class ArrayMatcherModelBuilder
|
||||
{
|
||||
public ArrayMatcherModelBuilder WithExactMatcher(object pattern, bool rejectOnMatch = false, bool ignoreCase = false)
|
||||
{
|
||||
return WithMatcher("ExactMatcher", pattern, rejectOnMatch, ignoreCase);
|
||||
}
|
||||
|
||||
public ArrayMatcherModelBuilder WithWildcardMatcher(object pattern, bool rejectOnMatch = false, bool ignoreCase = false)
|
||||
{
|
||||
return WithMatcher("WildcardMatcher", pattern, rejectOnMatch, ignoreCase);
|
||||
}
|
||||
|
||||
public ArrayMatcherModelBuilder WithRegexMatcher(object pattern, bool rejectOnMatch = false, bool ignoreCase = false)
|
||||
{
|
||||
return WithMatcher("RegexMatcher", pattern, rejectOnMatch, ignoreCase);
|
||||
}
|
||||
|
||||
public ArrayMatcherModelBuilder WithNotNullOrEmptyMatcher(bool rejectOnMatch = false)
|
||||
{
|
||||
return Add(mb => mb
|
||||
.WithName("NotNullOrEmptyMatcher")
|
||||
.WithRejectOnMatch(rejectOnMatch)
|
||||
);
|
||||
}
|
||||
|
||||
private ArrayMatcherModelBuilder WithMatcher(string name, object pattern, bool rejectOnMatch, bool ignoreCase = false)
|
||||
{
|
||||
return Add(mb => mb
|
||||
.WithName(name)
|
||||
.WithPattern(pattern)
|
||||
.WithRejectOnMatch(rejectOnMatch)
|
||||
.WithIgnoreCase(ignoreCase)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace WireMock.Admin.Mappings;
|
||||
|
||||
public partial class IListCookieModelBuilder
|
||||
{
|
||||
public IListCookieModelBuilder WithCookie(string name, Action<IListMatcherModelBuilder> action, bool rejectOnMatch = false)
|
||||
{
|
||||
return Add(cookieBuilder => cookieBuilder
|
||||
.WithName(name)
|
||||
.WithRejectOnMatch(rejectOnMatch)
|
||||
.WithMatchers(matchersBuilder => action(matchersBuilder))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace WireMock.Admin.Mappings;
|
||||
|
||||
public partial class IListHeaderModelBuilder
|
||||
{
|
||||
public IListHeaderModelBuilder WithHeader(string name, Action<IListMatcherModelBuilder> action, bool rejectOnMatch = false)
|
||||
{
|
||||
return Add(headerBuilder => headerBuilder
|
||||
.WithName(name)
|
||||
.WithRejectOnMatch(rejectOnMatch)
|
||||
.WithMatchers(matchersBuilder => action(matchersBuilder))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
namespace WireMock.Admin.Mappings;
|
||||
|
||||
public partial class IListMatcherModelBuilder
|
||||
{
|
||||
public IListMatcherModelBuilder WithExactMatcher(object pattern, bool rejectOnMatch = false, bool ignoreCase = false)
|
||||
{
|
||||
return WithMatcher("ExactMatcher", pattern, rejectOnMatch, ignoreCase);
|
||||
}
|
||||
|
||||
public IListMatcherModelBuilder WithWildcardMatcher(object pattern, bool rejectOnMatch = false, bool ignoreCase = false)
|
||||
{
|
||||
return WithMatcher("WildcardMatcher", pattern, rejectOnMatch, ignoreCase);
|
||||
}
|
||||
|
||||
public IListMatcherModelBuilder WithRegexMatcher(object pattern, bool rejectOnMatch = false, bool ignoreCase = false)
|
||||
{
|
||||
return WithMatcher("RegexMatcher", pattern, rejectOnMatch, ignoreCase);
|
||||
}
|
||||
|
||||
public IListMatcherModelBuilder WithNotNullOrEmptyMatcher(bool rejectOnMatch = false)
|
||||
{
|
||||
return Add(mb => mb
|
||||
.WithName("NotNullOrEmptyMatcher")
|
||||
.WithRejectOnMatch(rejectOnMatch)
|
||||
);
|
||||
}
|
||||
|
||||
private IListMatcherModelBuilder WithMatcher(string name, object pattern, bool rejectOnMatch, bool ignoreCase = false)
|
||||
{
|
||||
return Add(mb => mb
|
||||
.WithName(name)
|
||||
.WithPattern(pattern)
|
||||
.WithRejectOnMatch(rejectOnMatch)
|
||||
.WithIgnoreCase(ignoreCase)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright © WireMock.Net
|
||||
|
||||
using System;
|
||||
|
||||
namespace WireMock.Admin.Mappings;
|
||||
|
||||
public partial class IListParamModelBuilder
|
||||
{
|
||||
public IListParamModelBuilder WithParam(string name, Action<ArrayMatcherModelBuilder> action, bool rejectOnMatch = false)
|
||||
{
|
||||
return Add(paramBuilder => paramBuilder
|
||||
.WithName(name)
|
||||
.WithRejectOnMatch(rejectOnMatch)
|
||||
.WithMatchers(matchersBuilder => action(matchersBuilder))
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user