Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Data.Common;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
Expand All @@ -10,6 +11,7 @@
using JSONAPI.Json;
using Microsoft.Owin.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Owin;

namespace JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Tests
{
Expand All @@ -20,19 +22,18 @@ public abstract class AcceptanceTestsBase
private static readonly Regex GuidRegex = new Regex(@"\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b", RegexOptions.IgnoreCase);
//private static readonly Regex StackTraceRegex = new Regex(@"""stackTrace"":[\s]*""[\w\:\\\.\s\,\-]*""");
private static readonly Regex StackTraceRegex = new Regex(@"""stackTrace""[\s]*:[\s]*"".*?""");
private static readonly Uri BaseUri = new Uri("https://www.example.com");
protected static Uri BaseUri = new Uri("https://www.example.com");

protected static DbConnection GetEffortConnection()
{
return TestHelpers.GetEffortConnection(@"Data");
}

protected static async Task AssertResponseContent(HttpResponseMessage response, string expectedResponseTextResourcePath, HttpStatusCode expectedStatusCode, bool redactErrorData = false)
protected virtual async Task AssertResponseContent(HttpResponseMessage response, string expectedResponseTextResourcePath, HttpStatusCode expectedStatusCode, bool redactErrorData = false)
{
var responseContent = await response.Content.ReadAsStringAsync();

var expectedResponse =
JsonHelpers.MinifyJson(TestHelpers.ReadEmbeddedFile(expectedResponseTextResourcePath));
var expectedResponse = ExpectedResponse(expectedResponseTextResourcePath);
string actualResponse;
if (redactErrorData)
{
Expand All @@ -51,14 +52,21 @@ protected static async Task AssertResponseContent(HttpResponseMessage response,
response.StatusCode.Should().Be(expectedStatusCode);
}

protected virtual string ExpectedResponse(string expectedResponseTextResourcePath)
{
var expectedResponse =
JsonHelpers.MinifyJson(TestHelpers.ReadEmbeddedFile(expectedResponseTextResourcePath));
return expectedResponse;
}

#region GET

protected async Task<HttpResponseMessage> SubmitGet(DbConnection effortConnection, string requestPath)
{
using (var server = TestServer.Create(app =>
{
var startup = new Startup(() => new TestDbContext(effortConnection, false));
startup.Configuration(app);
StartupConfiguration(startup, app);
}))
{
var uri = new Uri(BaseUri, requestPath);
Expand All @@ -75,7 +83,7 @@ protected async Task<HttpResponseMessage> SubmitPost(DbConnection effortConnecti
using (var server = TestServer.Create(app =>
{
var startup = new Startup(() => new TestDbContext(effortConnection, false));
startup.Configuration(app);
StartupConfiguration(startup, app);
}))
{
var uri = new Uri(BaseUri, requestPath);
Expand All @@ -100,7 +108,7 @@ protected async Task<HttpResponseMessage> SubmitPatch(DbConnection effortConnect
using (var server = TestServer.Create(app =>
{
var startup = new Startup(() => new TestDbContext(effortConnection, false));
startup.Configuration(app);
StartupConfiguration(startup, app);
}))
{
var uri = new Uri(BaseUri, requestPath);
Expand All @@ -124,7 +132,7 @@ protected async Task<HttpResponseMessage> SubmitDelete(DbConnection effortConnec
using (var server = TestServer.Create(app =>
{
var startup = new Startup(() => new TestDbContext(effortConnection, false));
startup.Configuration(app);
StartupConfiguration(startup, app);
}))
{
var uri = new Uri(BaseUri, requestPath);
Expand All @@ -137,5 +145,22 @@ protected async Task<HttpResponseMessage> SubmitDelete(DbConnection effortConnec
}

#endregion



#region configure startup

/// <summary>
/// Startup process was divided into 4 steps to support better acceptance tests.
/// This method can be overridden by subclass to change behavior of setup.
/// </summary>
/// <param name="startup"></param>
/// <param name="app"></param>
protected virtual void StartupConfiguration(Startup startup, IAppBuilder app)
{
startup.Configuration(app);
}

#endregion
}
}
166 changes: 166 additions & 0 deletions JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Tests/BaseUrlTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using System;
using System.Data.SqlTypes;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using FluentAssertions;
using JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Models;
using JSONAPI.Http;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Owin;

namespace JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Tests
{
[TestClass]
public class BaseUrlTest : AcceptanceTestsBase
{
[TestInitialize]
public void TestInit()
{
if (!BaseUri.AbsoluteUri.EndsWith("api/"))
{
BaseUri = new Uri(BaseUri.AbsoluteUri + "api/");
}
}
[TestCleanup]
public void TestCleanup()
{
if (BaseUri.AbsoluteUri.EndsWith("api/"))
{
BaseUri = new Uri(BaseUri.AbsoluteUri.Substring(0,BaseUri.AbsoluteUri.Length -4));
}
}

// custom startup process for this test
protected override void StartupConfiguration(Startup startup, IAppBuilder app)
{

var configuration = startup.BuildConfiguration();
// here we add the custom BaseUrlServcie
configuration.CustomBaseUrlService = new BaseUrlService("api");
var configurator = startup.BuildAutofacConfigurator(app);
var httpConfig = startup.BuildHttpConfiguration();
startup.MergeAndSetupConfiguration(app, configurator, httpConfig, configuration);
}

// custom expected response method
protected override string ExpectedResponse(string expectedResponseTextResourcePath)
{
var expected = base.ExpectedResponse(expectedResponseTextResourcePath);
return Regex.Replace(expected, @"www\.example\.com\/", @"www.example.com/api/");
}

// copied some tests in here

// copied from ComputedIdTests

[TestMethod]
[DeploymentItem(@"Data\Comment.csv", @"Data")]
[DeploymentItem(@"Data\Language.csv", @"Data")]
[DeploymentItem(@"Data\LanguageUserLink.csv", @"Data")]
[DeploymentItem(@"Data\Post.csv", @"Data")]
[DeploymentItem(@"Data\PostTagLink.csv", @"Data")]
[DeploymentItem(@"Data\Tag.csv", @"Data")]
[DeploymentItem(@"Data\User.csv", @"Data")]
public async Task Get_resource_with_computed_id_by_id()
{
using (var effortConnection = GetEffortConnection())
{
var response = await SubmitGet(effortConnection, "language-user-links/9001_402");

await AssertResponseContent(response, @"Fixtures\ComputedId\Responses\Get_resource_with_computed_id_by_id_Response.json", HttpStatusCode.OK);
}
}


// copied from CreatingResourcesTests


[TestMethod]
[DeploymentItem(@"Data\PostLongId.csv", @"Data")]
public async Task PostLongId_with_client_provided_id()
{
using (var effortConnection = GetEffortConnection())
{
var response = await SubmitPost(effortConnection, "post-long-ids", @"Fixtures\CreatingResources\Requests\PostLongId_with_client_provided_id_Request.json");

await AssertResponseContent(response, @"Fixtures\CreatingResources\Responses\PostLongId_with_client_provided_id_Response.json", HttpStatusCode.OK);

using (var dbContext = new TestDbContext(effortConnection, false))
{
var allPosts = dbContext.PostsLongId.ToArray();
allPosts.Length.Should().Be(5);
var actualPost = allPosts.First(t => t.Id == 205);
actualPost.Id.Should().Be(205);
actualPost.Title.Should().Be("Added post");
actualPost.Content.Should().Be("Added post content");
actualPost.Created.Should().Be(new DateTimeOffset(2015, 03, 11, 04, 31, 0, new TimeSpan(0)));
}
}
}



// copied from DeletingResourcesTests

[TestMethod]
[DeploymentItem(@"Data\PostID.csv", @"Data")]
public async Task DeleteID()
{
using (var effortConnection = GetEffortConnection())
{
var response = await SubmitDelete(effortConnection, "post-i-ds/203");

var responseContent = await response.Content.ReadAsStringAsync();
responseContent.Should().Be("");
response.StatusCode.Should().Be(HttpStatusCode.NoContent);

using (var dbContext = new TestDbContext(effortConnection, false))
{
var allPosts = dbContext.PostsID.ToArray();
allPosts.Length.Should().Be(3);
var actualPosts = allPosts.FirstOrDefault(t => t.ID == "203");
actualPosts.Should().BeNull();
}
}
}



// copied from FetchingResourcesTests

[TestMethod]
[DeploymentItem(@"Data\Comment.csv", @"Data")]
[DeploymentItem(@"Data\Post.csv", @"Data")]
[DeploymentItem(@"Data\PostTagLink.csv", @"Data")]
[DeploymentItem(@"Data\Tag.csv", @"Data")]
[DeploymentItem(@"Data\User.csv", @"Data")]
public async Task GetWithFilter()
{
using (var effortConnection = GetEffortConnection())
{
var response = await SubmitGet(effortConnection, "posts?filter[title]=Post 4");

await AssertResponseContent(response, @"Fixtures\FetchingResources\GetWithFilterResponse.json", HttpStatusCode.OK);
}
}

[TestMethod]
[DeploymentItem(@"Data\Comment.csv", @"Data")]
[DeploymentItem(@"Data\Post.csv", @"Data")]
[DeploymentItem(@"Data\PostTagLink.csv", @"Data")]
[DeploymentItem(@"Data\Tag.csv", @"Data")]
[DeploymentItem(@"Data\User.csv", @"Data")]
public async Task GetById()
{
using (var effortConnection = GetEffortConnection())
{
var response = await SubmitGet(effortConnection, "posts/202");

await AssertResponseContent(response, @"Fixtures\FetchingResources\GetByIdResponse.json", HttpStatusCode.OK);
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
<Reference Include="Microsoft.Owin.Testing">
<HintPath>..\packages\Microsoft.Owin.Testing.3.0.0\lib\net45\Microsoft.Owin.Testing.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NMemory">
<HintPath>..\packages\NMemory.1.0.1\lib\net45\NMemory.dll</HintPath>
</Reference>
Expand All @@ -71,7 +75,15 @@
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\net45\System.Net.Http.Formatting.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Transactions" />
<Reference Include="System.Web.Http, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.2\lib\net45\System.Web.Http.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
Expand All @@ -98,6 +110,7 @@
<Compile Include="HeterogeneousTests.cs" />
<Compile Include="MappedTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="BaseUrlTest.cs" />
<Compile Include="SortingTests.cs" />
<Compile Include="TestHelpers.cs" />
<Compile Include="UpdatingResourcesTests.cs" />
Expand All @@ -107,6 +120,10 @@
<Project>{76dee472-723b-4be6-8b97-428ac326e30f}</Project>
<Name>JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp</Name>
</ProjectReference>
<ProjectReference Include="..\JSONAPI.Autofac\JSONAPI.Autofac.csproj">
<Project>{AF7861F3-550B-4F70-A33E-1E5F48D39333}</Project>
<Name>JSONAPI.Autofac</Name>
</ProjectReference>
<ProjectReference Include="..\JSONAPI\JSONAPI.csproj">
<Project>{52b19fd6-efaa-45b5-9c3e-a652e27608d1}</Project>
<Name>JSONAPI</Name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
<package id="Effort.EF6" version="1.1.4" targetFramework="net45" />
<package id="EntityFramework" version="6.1.3" targetFramework="net45" />
<package id="FluentAssertions" version="3.2.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.Owin" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.Owin.Hosting" version="3.0.0" targetFramework="net45" />
<package id="Microsoft.Owin.Testing" version="3.0.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
<package id="NMemory" version="1.0.1" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" />
</packages>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand Down Expand Up @@ -32,3 +33,4 @@
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: InternalsVisibleTo("JSONAPI.AcceptanceTests.EntityFrameworkTestWebApp.Tests")]
Loading