mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-14 22:33:35 +01:00
81 lines
3.4 KiB
C#
81 lines
3.4 KiB
C#
using System;
|
|
using Newtonsoft.Json;
|
|
using WireMock.Matchers;
|
|
using WireMock.RequestBuilders;
|
|
using WireMock.ResponseBuilders;
|
|
using WireMock.Server;
|
|
|
|
|
|
namespace WireMock.Net.ConsoleApplication
|
|
{
|
|
static class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
int port;
|
|
if (args.Length == 0 || !int.TryParse(args[0], out port))
|
|
port = 8080;
|
|
|
|
var server = FluentMockServer.Start(port);
|
|
Console.WriteLine("FluentMockServer running at {0}", server.Port);
|
|
|
|
server
|
|
.Given(Request.Create().WithUrl(u => u.Contains("x")).UsingGet())
|
|
.RespondWith(Response.Create()
|
|
.WithStatusCode(200)
|
|
.WithHeader("Content-Type", "application/json")
|
|
.WithBody(@"{ ""result"": ""/x with FUNC 200""}"));
|
|
|
|
// http://localhost:8080/gffgfgf/sddsds?start=1000&stop=1&stop=2
|
|
server
|
|
.Given(Request.Create().WithUrl("/*").UsingGet())
|
|
.RespondWith(Response.Create()
|
|
.WithStatusCode(200)
|
|
.WithHeader("Content-Type", "application/json")
|
|
.WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}")
|
|
.WithBody(@"{""msg"": ""Hello world! : {{request.url}} : {{request.path}} :
|
|
bykey={{request.query.start}} : bykey={{request.query.stop}} byidx0={{request.query.stop.[0]}} byidx1={{request.query.stop.[1]}}""")
|
|
.WithTransformer()
|
|
.WithDelay(TimeSpan.FromMilliseconds(100))
|
|
);
|
|
|
|
server
|
|
.Given(Request.Create().WithUrl("/data").UsingPost().WithBody(b => b.Contains("e")))
|
|
.RespondWith(Response.Create()
|
|
.WithStatusCode(201)
|
|
.WithHeader("Content-Type", "application/json")
|
|
.WithBody(@"{ ""result"": ""data posted with FUNC 201""}"));
|
|
|
|
server
|
|
.Given(Request.Create().WithUrl("/data", "/ax").UsingPost())
|
|
.RespondWith(Response.Create()
|
|
.WithStatusCode(201)
|
|
.WithHeader("Content-Type", "application/json")
|
|
.WithBody(@"{ ""result"": ""data posted with 201""}"));
|
|
|
|
server
|
|
.Given(Request.Create().WithUrl("/json").UsingPost().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")))
|
|
.RespondWith(Response.Create()
|
|
.WithStatusCode(201)
|
|
.WithHeader("Content-Type", "application/json")
|
|
.WithBody(@"{ ""result"": ""json posted with 201""}"));
|
|
|
|
server
|
|
.Given(Request.Create().WithUrl("/data").UsingDelete())
|
|
.RespondWith(Response.Create()
|
|
.WithStatusCode(200)
|
|
.WithHeader("Content-Type", "application/json")
|
|
.WithBody(@"{ ""result"": ""data deleted with 200""}"));
|
|
|
|
Console.WriteLine("Press any key to stop the server");
|
|
Console.ReadKey();
|
|
|
|
Console.WriteLine("Displaying all requests");
|
|
var allRequests = server.RequestLogs;
|
|
Console.WriteLine(JsonConvert.SerializeObject(allRequests, Formatting.Indented));
|
|
|
|
Console.WriteLine("Press any key to quit");
|
|
Console.ReadKey();
|
|
}
|
|
}
|
|
} |