Skip to content

Commit a6341a5

Browse files
committed
All arg params are now switches, re-added integration tests
1 parent d98638b commit a6341a5

10 files changed

+303
-135
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@ The [end user documentation](netmockery/documentation.md) is a work in progress.
55
[![Build Status](https://travis-ci.org/codeape2/netmockery.svg?branch=master)](https://travis-ci.org/codeape2/netmockery)
66

77

8-
BUILDING AND TESTING
8+
TESTING
99
====================
10+
```
11+
dotnet test .\netmockery.sln
12+
```
1013

1114

15+
RUNNING
16+
====================
1217
```
13-
dotnet test .\netmockery.sln
18+
dotnet run --project .\netmockery\netmockery.csproj --command web --endpoints C:\Projects\NHN\Helsenorge\Test\netmockery_config\helsenorge_endpoints --urls http://*:9876
1419
```
1520

21+
1622
DEPLOYMENT PACKAGES
1723
===================
18-
1924
```
2025
dotnet publish .\netmockery.sln
2126
```

UnitTests/TestCommandLineParser.cs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,63 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using netmockery;
1+
using netmockery;
62
using Xunit;
73

84
using static netmockery.CommandLineParser;
95

106

11-
127
namespace UnitTests
138
{
149
public class TestCommandLineParser
1510
{
1611
[Fact]
17-
public void ParsesWithNoCommand()
12+
public void WebCommand()
1813
{
19-
var result = ParseArguments(new[] { "c:\\dir\\foo" });
20-
21-
Assert.Equal(COMMAND_NORMAL, result.Command);
22-
Assert.Equal("c:\\dir\\foo", result.EndpointCollectionDirectory);
14+
var result = ParseArguments(new[] { "--command", "web", "--endpoints", "c:\\dir\\foo" });
15+
Assert.Equal(COMMAND_WEB, result.Command);
16+
Assert.Equal("c:\\dir\\foo", result.Endpoints);
2317
Assert.Null(result.Urls);
2418
}
2519

2620
[Fact]
27-
public void NormalNoTestMode()
21+
public void WebCommandNoTestMode()
2822
{
29-
var result = ParseArguments(new[] { "c:\\dir\\foo", "--notestmode" });
30-
Assert.Equal(COMMAND_NORMAL, result.Command);
31-
Assert.Equal("c:\\dir\\foo", result.EndpointCollectionDirectory);
23+
var result = ParseArguments(new[] { "--command", "web", "--endpoints", "c:\\dir\\foo", "--notestmode" });
24+
Assert.Equal(COMMAND_WEB, result.Command);
25+
Assert.Equal("c:\\dir\\foo", result.Endpoints);
3226
Assert.True(result.NoTestMode);
3327

3428
}
3529

3630
[Fact]
37-
public void NormalExecutionCanHaveUrl()
31+
public void WebCommandWithUrls()
3832
{
39-
var result = ParseArguments(new[] { "c:\\dir\\foo", "--urls", "http://*:5000" });
40-
Assert.Equal(COMMAND_NORMAL, result.Command);
41-
Assert.Equal("c:\\dir\\foo", result.EndpointCollectionDirectory);
33+
var result = ParseArguments(new[] { "--command", "web", "--endpoints", "c:\\dir\\foo", "--urls", "http://*:5000" });
34+
Assert.Equal(COMMAND_WEB, result.Command);
35+
Assert.Equal("c:\\dir\\foo", result.Endpoints);
4236
Assert.Equal("http://*:5000", result.Urls);
4337
}
4438

4539
[Fact]
46-
public void MissingEndpointDirectoryGivesError()
40+
public void MissingCommand()
4741
{
48-
AssertGivesException("No endpoint directory specified", new string[0]);
42+
AssertGivesException("Missing required switch --command", new[] { "--endpoints", "c:\\foo\\bar" });
4943
}
5044

5145
[Fact]
5246
public void UnknownCommand()
5347
{
54-
AssertGivesException("Unknown command 'foobar'", new[] { "c:\\foo\\bar", "foobar" });
48+
AssertGivesException("Unknown command 'foobar'", new[] { "--command", "foobar", "--endpoints", "c:\\foo\\bar" });
49+
}
50+
51+
[Fact]
52+
public void MissingEndpoints()
53+
{
54+
AssertGivesException("Missing required switch --endpoints", new[] { "--command", "web" });
5555
}
5656

5757
[Fact]
5858
public void TestCommand()
5959
{
60-
var result = ParseArguments(new[] { "c:\\dir\\foo", "test" });
60+
var result = ParseArguments(new[] { "--command", "test", "--endpoints", "c:\\dir\\foo" });
6161
Assert.Equal(COMMAND_TEST, result.Command);
6262
Assert.False(result.ShowResponse);
6363
Assert.Null(result.Only);
@@ -66,7 +66,7 @@ public void TestCommand()
6666
[Fact]
6767
public void TestCommandWithOptions()
6868
{
69-
var result = ParseArguments(new[] { "c:\\dir\\foo", "test", "--only", "1", "--showResponse" });
69+
var result = ParseArguments(new[] { "--command", "test", "--endpoints", "c:\\dir\\foo", "--only", "1", "--showResponse" });
7070
Assert.Equal(COMMAND_TEST, result.Command);
7171
Assert.Equal("1", result.Only);
7272
Assert.True(result.ShowResponse);
@@ -76,7 +76,7 @@ public void TestCommandWithOptions()
7676
[Fact]
7777
public void TestWithStopOption()
7878
{
79-
var result = ParseArguments(new[] { "c:\\dir\\foo", "test", "--stop" });
79+
var result = ParseArguments(new[] { "--command", "test", "--endpoints", "c:\\dir\\foo", "--stop" });
8080
Assert.Equal(COMMAND_TEST, result.Command);
8181
Assert.Null(result.Only);
8282
Assert.False(result.ShowResponse);
@@ -86,22 +86,22 @@ public void TestWithStopOption()
8686
[Fact]
8787
public void DumpCommand()
8888
{
89-
var result = ParseArguments(new[] { "c:\\foo\\bar", "dump" });
89+
var result = ParseArguments(new[] { "--command", "dump", "--endpoints", "c:\\foo\\bar" });
9090
Assert.Equal(COMMAND_DUMP, result.Command);
91-
Assert.Equal("c:\\foo\\bar", result.EndpointCollectionDirectory);
91+
Assert.Equal("c:\\foo\\bar", result.Endpoints);
9292
}
9393

9494
[Fact]
9595
public void InvalidArgumentsForDumpCommand()
9696
{
97-
AssertGivesException("'--only' is not a valid argument for the 'dump' command", new[] { "c:\\foo\\bar", "dump", "--only", "2" });
98-
AssertGivesException("'--urls' is not a valid argument for the 'dump' command", new[] { "c:\\foo\\bar", "dump", "--urls", "http://localhost:5000/" });
97+
AssertGivesException("'--only' is not a valid argument for the 'dump' command", new[] { "--command", "dump", "--endpoints", "c:\\foo\\bar", "--only", "2" });
98+
AssertGivesException("'--urls' is not a valid argument for the 'dump' command", new[] { "--command", "dump", "--endpoints", "c:\\foo\\bar", "--urls", "http://localhost:5000/" });
9999
}
100100

101101
[Fact]
102-
public void InvalidArgumentsForNormalCommand()
102+
public void InvalidArgumentsForWebCommand()
103103
{
104-
AssertGivesException("'--only' is not a valid argument", new[] { "c:\\foo\\bar", "--only", "2" });
104+
AssertGivesException("'--only' is not a valid argument for the 'web' command", new[] { "--command", "web", "--endpoints", "c:\\foo\\bar", "--only", "2" });
105105
}
106106

107107
private void AssertGivesException(string expectedMessage, string[] args)

UnitTests/TestReloadWorks.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using Microsoft.AspNetCore.Mvc.Testing;
2+
using netmockery;
3+
using Newtonsoft.Json;
4+
using System;
5+
using System.Linq;
6+
using System.Net;
7+
using System.Net.Http;
8+
using System.Threading.Tasks;
9+
using Xunit;
10+
11+
namespace UnitTests
12+
{
13+
public class TestReloadWorks : IDisposable
14+
{
15+
private readonly DirectoryCreator _dc;
16+
private readonly HttpClient _client;
17+
18+
public TestReloadWorks()
19+
{
20+
_dc = new DirectoryCreator();
21+
_dc.AddFile(
22+
"endpoint1/endpoint.json",
23+
JsonConvert.SerializeObject(DataUtils.CreateSimpleEndpoint("foobar", "myfile.txt"))
24+
);
25+
_dc.AddFile("endpoint1/myfile.txt", "Hello world");
26+
var ecp = new EndpointCollectionProvider(_dc.DirectoryName);
27+
28+
var factory = new CustomWebApplicationFactory<Program>(ecp);
29+
_client = factory.CreateClient(new WebApplicationFactoryClientOptions
30+
{
31+
AllowAutoRedirect = false,
32+
});
33+
}
34+
35+
public void Dispose()
36+
{
37+
_dc.Dispose();
38+
}
39+
40+
[Fact]
41+
public async Task CanListEndpointnames()
42+
{
43+
Assert.Equal(new[] { "foobar" }, await GetEndpointNames());
44+
}
45+
46+
[Fact]
47+
public async Task NewEndpointIsAvailableAfterReload()
48+
{
49+
Assert.Equal(new[] { "foobar" }, await GetEndpointNames());
50+
51+
_dc.AddFile(
52+
"endpoint2/endpoint.json",
53+
JsonConvert.SerializeObject(DataUtils.CreateSimpleEndpoint("baz", "myfile.txt"))
54+
);
55+
_dc.AddFile("endpoint2/myfile.txt", "Hello world");
56+
57+
Assert.Equal(new[] { "foobar" }, await GetEndpointNames());
58+
59+
var response = await _client.GetAsync("/__netmockery/endpoints/reloadconfig");
60+
Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
61+
62+
Assert.Equal(new[] { "baz", "foobar" }, await GetEndpointNames());
63+
}
64+
65+
private async Task<string[]> GetEndpointNames()
66+
{
67+
var response = await _client.GetAsync("/__netmockery/endpoints/endpointnames");
68+
response.EnsureSuccessStatusCode();
69+
return (from arrayitem in JsonConvert.DeserializeObject<string[]>(await response.Content.ReadAsStringAsync()) orderby arrayitem select arrayitem).ToArray();
70+
}
71+
}
72+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using Microsoft.AspNetCore.Mvc.Testing;
2+
using netmockery;
3+
using Newtonsoft.Json;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Net.Http;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using Xunit;
10+
11+
namespace UnitTests
12+
{
13+
public class TestResponseHasCorrectEncoding : IDisposable
14+
{
15+
private readonly DirectoryCreator _dc;
16+
private readonly HttpClient _client;
17+
18+
public TestResponseHasCorrectEncoding()
19+
{
20+
_dc = new DirectoryCreator();
21+
_dc.AddFile("endpoint1/endpoint.json", JsonConvert.SerializeObject(DataUtils.CreateSimpleEndpoint("endpoint1", "myfile.txt", "/endpoint1")));
22+
_dc.AddFile("endpoint1/myfile.txt", "æøå");
23+
24+
var endpoint2 = DataUtils.CreateSimpleEndpoint("endpoint2", "myfile.txt", "/endpoint2");
25+
endpoint2.responses[0].charset = "latin1";
26+
_dc.AddFile("endpoint2/endpoint.json", JsonConvert.SerializeObject(endpoint2));
27+
_dc.AddFile("endpoint2/myfile.txt", "æøå");
28+
var tests = new List<JSONTest>(new[] {
29+
new JSONTest { name = "Test endpoint1", requestpath = "/endpoint1", expectedresponsebody = "æøå" }
30+
});
31+
_dc.AddFile("tests/tests.json", JsonConvert.SerializeObject(tests));
32+
33+
var ecp = new EndpointCollectionProvider(_dc.DirectoryName);
34+
35+
var factory = new CustomWebApplicationFactory<Program>(ecp);
36+
_client = factory.CreateClient(new WebApplicationFactoryClientOptions
37+
{
38+
AllowAutoRedirect = false,
39+
});
40+
}
41+
42+
public void Dispose()
43+
{
44+
_dc.Dispose();
45+
}
46+
47+
[Fact]
48+
async public Task ResponseHasUtf8EncodingIfNotConfigured()
49+
{
50+
var response = await _client.GetAsync("/endpoint1");
51+
var bytes = await response.Content.ReadAsByteArrayAsync();
52+
Assert.Equal("æøå", DecodeUtf8(bytes));
53+
Assert.NotEqual("æøå", DecodeLatin1(bytes));
54+
}
55+
56+
private string DecodeLatin1(byte[] bytes)
57+
{
58+
return Encoding.GetEncoding("ISO-8859-1").GetString(bytes);
59+
}
60+
61+
private string DecodeUtf8(byte[] bytes)
62+
{
63+
return Encoding.UTF8.GetString(bytes);
64+
}
65+
66+
[Fact]
67+
async public Task ResponseCanBeLatin1IfConfigured()
68+
{
69+
var response = await _client.GetAsync("/endpoint2");
70+
var bytes = await response.Content.ReadAsByteArrayAsync();
71+
Assert.Equal("æøå", DecodeLatin1(bytes));
72+
Assert.NotEqual("æøå", DecodeUtf8(bytes));
73+
}
74+
75+
[Fact]
76+
async public Task ResponseHasCharsetInHeader()
77+
{
78+
var response = await _client.GetAsync("/endpoint1");
79+
Assert.Equal("text/plain", response.Content.Headers.ContentType.MediaType);
80+
Assert.Equal("utf-8", response.Content.Headers.ContentType.CharSet);
81+
82+
response = await _client.GetAsync("/endpoint2");
83+
Assert.Equal("text/plain", response.Content.Headers.ContentType.MediaType);
84+
Assert.Equal("iso-8859-1", response.Content.Headers.ContentType.CharSet);
85+
}
86+
}
87+
}

UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</PropertyGroup>
66
<ItemGroup>
77
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
8+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.14" />
89
<PackageReference Include="xunit" Version="2.4.2" />
910
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
1011
</ItemGroup>

UnitTests/WebTestBase.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using netmockery;
4+
using Microsoft.AspNetCore.Mvc.Testing;
5+
6+
7+
namespace UnitTests
8+
{
9+
public class CustomWebApplicationFactory<TProgram> : WebApplicationFactory<TProgram> where TProgram : class
10+
{
11+
private EndpointCollectionProvider _endpointCollectionProvider;
12+
13+
public CustomWebApplicationFactory(EndpointCollectionProvider endpointCollectionProvider) : base()
14+
{
15+
_endpointCollectionProvider = endpointCollectionProvider;
16+
}
17+
18+
protected override void ConfigureWebHost(IWebHostBuilder builder)
19+
{
20+
builder.UseSetting("command", "web");
21+
builder.UseSetting("endpoints", ".."); // EndpointCollectionProvider is mocked anyway
22+
23+
builder.ConfigureServices(services =>
24+
{
25+
services.AddTransient(serviceProvider => _endpointCollectionProvider);
26+
});
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)