Skip to content

Commit

Permalink
Update and simplify the sample
Browse files Browse the repository at this point in the history
  • Loading branch information
markvincze committed Oct 17, 2017
1 parent 9fa08ff commit 3d7fcbd
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 100 deletions.
Expand Up @@ -31,18 +31,18 @@ public async Task<IActionResult> GetCountyName(string postcode)

var response = await hc.GetAsync(uri);

using(var stream = await response.Content.ReadAsStreamAsync())
using (var stream = await response.Content.ReadAsStreamAsync())
using (var reader = new StreamReader(stream))
using (var jsonTextReader = new JsonTextReader(reader))
{
var jObject = JObject.Load(jsonTextReader);

if (jObject["status"].ToString() == "200")
if (jObject["status"].Value<string>() == "200")
{
return Ok(jObject["result"]["admin_county"].ToString());
return Ok(jObject["result"]["admin_county"].Value<string>());
}

if (jObject["status"].ToString() == "404")
if (jObject["status"].Value<string>() == "404")
{
return NotFound();
}
Expand Down
@@ -1,4 +1,4 @@
using System.IO;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace Stubbery.Samples.BasicSample.Web
Expand All @@ -7,14 +7,12 @@ public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://*:5000/")
.UseContentRoot(Directory.GetCurrentDirectory())
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();

host.Run();
}
}
}
Expand Up @@ -8,23 +8,18 @@ namespace Stubbery.Samples.BasicSample.Web
{
public class Startup
{
public Startup(IHostingEnvironment env)
public Startup(IConfiguration config)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.SetBasePath(env.ContentRootPath);

Configuration = builder.Build();
Configuration = config;
}

public IConfigurationRoot Configuration { get; set; }
public IConfiguration Configuration { get; set; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<IConfiguration>(Configuration);
services.AddSingleton(Configuration);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
Expand Down
@@ -1,24 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>Stubbery.Samples.BasicSample.Web</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>Stubbery.Samples.BasicSample.Web</PackageId>
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="1.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="1.0.2" />
<PackageReference Include="System.IO.FileSystem" Version="4.0.1" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
</ItemGroup>

</Project>
@@ -1,38 +1,20 @@
using System.Net;
using System.Collections.Generic;
using System.Net;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
using Stubbery.Samples.BasicSample.Web;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.Extensions.Configuration.Memory;
using Xunit;

namespace Stubbery.Samples.BasicSample.IntegrationTests
{
public class CountyTests
{
private readonly TestServer server;

private readonly ApiStub postcodeApiStub;

private IHostingEnvironment CreateHostingEnvironment()
{
var hostingEnvironment = new HostingEnvironment();

var appEnvironment = PlatformServices.Default.Application;

var applicationName = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

hostingEnvironment.Initialize(applicationName, appEnvironment.ApplicationBasePath, new WebHostOptions());

return hostingEnvironment;
}

public CountyTests()
private ApiStub StartStub()
{
postcodeApiStub = new ApiStub();
var postcodeApiStub = new ApiStub();

postcodeApiStub.Get(
"/postcodes/{postcode}",
Expand All @@ -53,27 +35,35 @@ public CountyTests()

postcodeApiStub.Start();

var hostingEnv = CreateHostingEnvironment();
var loggerFactory = new LoggerFactory();

var startup = new Startup(hostingEnv);
return postcodeApiStub;
}

server = new TestServer(new WebHostBuilder()
.Configure(app => startup.Configure(app, hostingEnv, loggerFactory))
.ConfigureServices(
services =>
private TestServer StartApiUnderTest(ApiStub postcodeApiStub)
{
var server = new TestServer(
WebHost.CreateDefaultBuilder()
.UseStartup<Startup>()
.ConfigureAppConfiguration((ctx, b) =>
{
startup.ConfigureServices(services);
// Replace the real api URL with the stub.
startup.Configuration["PostCodeApiUrl"] = postcodeApiStub.Address;
b.Add(new MemoryConfigurationSource
{
InitialData = new Dictionary<string, string>
{
// Replace the real api URL with the stub.
["PostCodeApiUrl"] = postcodeApiStub.Address
}
});
}));

return server;
}

[Fact]
public async Task GetCountyName_CountyFound_CountyNameReturned()
{
using (var client = server.CreateClient().AcceptJson())
using (var stub = StartStub())
using (var server = StartApiUnderTest(stub))
using (var client = server.CreateClient())
{
var response = await client.GetAsync("/countyname/postcodeOk");

Expand All @@ -86,7 +76,9 @@ public async Task GetCountyName_CountyFound_CountyNameReturned()
[Fact]
public async Task GetCountyName_CountyNotFound_NotFoundReturned()
{
using (var client = server.CreateClient().AcceptJson())
using (var stub = StartStub())
using (var server = StartApiUnderTest(stub))
using (var client = server.CreateClient())
{
var response = await client.GetAsync("/countyname/postcodeNotFound");

Expand All @@ -97,7 +89,9 @@ public async Task GetCountyName_CountyNotFound_NotFoundReturned()
[Fact]
public async Task GetCountyName_Error_InternalServerErrorReturned()
{
using (var client = server.CreateClient().AcceptJson())
using (var stub = StartStub())
using (var server = StartApiUnderTest(stub))
using (var client = server.CreateClient())
{
var response = await client.GetAsync("/countyname/postcodeError");

Expand Down

This file was deleted.

@@ -1,12 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<AssemblyName>Stubbery.Samples.BasicSample.IntegrationTests</AssemblyName>
<PackageId>Stubbery.Samples.BasicSample.IntegrationTests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50;portable-net45+win8</PackageTargetFallback>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
Expand All @@ -20,13 +16,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0-beta5-build1225" />
<PackageReference Include="xunit" Version="2.2.0-beta5-build3474" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.0.2" />
<PackageReference Include="Stubbery" Version="1.2.3" />
<PackageReference Include="Microsoft.Extensions.Options" Version="1.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0" />
<PackageReference Include="xunit" Version="2.3.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.0" />
<PackageReference Include="Stubbery" Version="1.2.6" />
</ItemGroup>

</Project>

0 comments on commit 3d7fcbd

Please sign in to comment.