11using System ;
22using System . Data . Common ;
3- using System . Net ;
43using System . Net . Http ;
54using System . Text ;
65using System . Text . RegularExpressions ;
@@ -18,40 +17,30 @@ namespace JSONAPI.EntityFramework.Tests.Acceptance
1817 public abstract class AcceptanceTestsBase
1918 {
2019 private static readonly Regex GuidRegex = new Regex ( @"\b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12}\b" , RegexOptions . IgnoreCase ) ;
20+ //private static readonly Regex StackTraceRegex = new Regex(@"""stackTrace"":[\s]*""[\w\:\\\.\s\,\-]*""");
21+ private static readonly Regex StackTraceRegex = new Regex ( @"""stackTrace""[\s]*:[\s]*"".*?""" ) ;
2122 private static readonly Uri BaseUri = new Uri ( "http://localhost" ) ;
2223
2324 protected static DbConnection GetEffortConnection ( )
2425 {
2526 return TestHelpers . GetEffortConnection ( @"Acceptance\Data" ) ;
2627 }
2728
28- protected async Task ExpectGetToSucceed ( DbConnection effortConnection , string requestPath ,
29- string expectedResponseTextResourcePath )
29+ protected async Task AssertResponseContent ( HttpResponseMessage response , string expectedResponseTextResourcePath )
3030 {
31- var response = await GetEndpointResponse ( effortConnection , requestPath ) ;
32-
33- response . StatusCode . Should ( ) . Be ( HttpStatusCode . OK ) ;
3431 var responseContent = await response . Content . ReadAsStringAsync ( ) ;
3532
36- var expected =
33+ var expectedResponse =
3734 JsonHelpers . MinifyJson ( TestHelpers . ReadEmbeddedFile ( expectedResponseTextResourcePath ) ) ;
38- responseContent . Should ( ) . Be ( expected ) ;
39- }
40-
41- protected async Task ExpectGetToFail ( DbConnection effortConnection , string requestPath ,
42- string expectedResponseTextResourcePath , HttpStatusCode expectedStatusCode = HttpStatusCode . BadRequest )
43- {
44- var expectedResponse = JsonHelpers . MinifyJson ( TestHelpers . ReadEmbeddedFile ( expectedResponseTextResourcePath ) ) ;
45- var response = await GetEndpointResponse ( effortConnection , requestPath ) ;
46- response . StatusCode . Should ( ) . Be ( expectedStatusCode ) ;
47-
48- var responseContent = await response . Content . ReadAsStringAsync ( ) ;
4935 var redactedResponse = GuidRegex . Replace ( responseContent , "{{SOME_GUID}}" ) ;
36+ redactedResponse = StackTraceRegex . Replace ( redactedResponse , "\" stackTrace\" :\" {{STACK_TRACE}}\" " ) ;
5037
5138 redactedResponse . Should ( ) . Be ( expectedResponse ) ;
5239 }
5340
54- protected async Task < HttpResponseMessage > GetEndpointResponse ( DbConnection effortConnection , string requestPath )
41+ #region GET
42+
43+ protected async Task < HttpResponseMessage > SubmitGet ( DbConnection effortConnection , string requestPath )
5544 {
5645 using ( var server = TestServer . Create ( app =>
5746 {
@@ -65,26 +54,10 @@ protected async Task<HttpResponseMessage> GetEndpointResponse(DbConnection effor
6554 }
6655 }
6756
68- protected async Task TestGetWithFilter ( DbConnection effortConnection , string requestPath , string expectedResponseTextResourcePath )
69- {
70- using ( var server = TestServer . Create ( app =>
71- {
72- var startup = new Startup ( context => new TestDbContext ( effortConnection , false ) ) ;
73- startup . Configuration ( app ) ;
74- } ) )
75- {
76- var uri = new Uri ( BaseUri , requestPath ) ;
77- var response = await server . CreateRequest ( uri . ToString ( ) ) . GetAsync ( ) ;
78- response . StatusCode . Should ( ) . Be ( HttpStatusCode . OK ) ;
79- var responseContent = await response . Content . ReadAsStringAsync ( ) ;
57+ #endregion
58+ #region POST
8059
81- var expected =
82- JsonHelpers . MinifyJson ( TestHelpers . ReadEmbeddedFile ( expectedResponseTextResourcePath ) ) ;
83- responseContent . Should ( ) . Be ( expected ) ;
84- }
85- }
86-
87- protected async Task TestGetById ( DbConnection effortConnection , string requestPath , string expectedResponseTextResourcePath )
60+ protected async Task < HttpResponseMessage > SubmitPost ( DbConnection effortConnection , string requestPath , string requestDataTextResourcePath )
8861 {
8962 using ( var server = TestServer . Create ( app =>
9063 {
@@ -93,45 +66,22 @@ protected async Task TestGetById(DbConnection effortConnection, string requestPa
9366 } ) )
9467 {
9568 var uri = new Uri ( BaseUri , requestPath ) ;
96- var response = await server . CreateRequest ( uri . ToString ( ) ) . GetAsync ( ) ;
97- response . StatusCode . Should ( ) . Be ( HttpStatusCode . OK ) ;
98- var responseContent = await response . Content . ReadAsStringAsync ( ) ;
99-
100- var expected =
101- JsonHelpers . MinifyJson ( TestHelpers . ReadEmbeddedFile ( expectedResponseTextResourcePath ) ) ;
102- responseContent . Should ( ) . Be ( expected ) ;
103- }
104- }
105-
106- protected async Task TestPost ( DbConnection effortConnection , string requestPath , string requestDataTextResourcePath , string expectedResponseTextResourcePath )
107- {
108- using ( var server = TestServer . Create ( app =>
109- {
110- var startup = new Startup ( context => new TestDbContext ( effortConnection , false ) ) ;
111- startup . Configuration ( app ) ;
112- } ) )
113- {
114- var uri = new Uri ( BaseUri , requestPath ) ;
115- var requestContent =
116- JsonHelpers . MinifyJson (
117- TestHelpers . ReadEmbeddedFile ( requestDataTextResourcePath ) ) ;
69+ var requestContent = TestHelpers . ReadEmbeddedFile ( requestDataTextResourcePath ) ;
11870 var response = await server
11971 . CreateRequest ( uri . ToString ( ) )
12072 . And ( request =>
12173 {
12274 request . Content = new StringContent ( requestContent , Encoding . UTF8 , "application/vnd.api+json" ) ;
12375 } )
12476 . PostAsync ( ) ;
125- response . StatusCode . Should ( ) . Be ( HttpStatusCode . OK ) ;
126- var responseContent = await response . Content . ReadAsStringAsync ( ) ;
127-
128- var expected =
129- JsonHelpers . MinifyJson ( TestHelpers . ReadEmbeddedFile ( expectedResponseTextResourcePath ) ) ;
130- responseContent . Should ( ) . Be ( expected ) ;
77+ return response ;
13178 }
13279 }
13380
134- protected async Task TestPut ( DbConnection effortConnection , string requestPath , string requestDataTextResourcePath , string expectedResponseTextResourcePath )
81+ #endregion
82+ #region PUT
83+
84+ protected async Task < HttpResponseMessage > SubmitPut ( DbConnection effortConnection , string requestPath , string requestDataTextResourcePath )
13585 {
13686 using ( var server = TestServer . Create ( app =>
13787 {
@@ -140,26 +90,21 @@ protected async Task TestPut(DbConnection effortConnection, string requestPath,
14090 } ) )
14191 {
14292 var uri = new Uri ( BaseUri , requestPath ) ;
143- var requestContent =
144- JsonHelpers . MinifyJson (
145- TestHelpers . ReadEmbeddedFile ( requestDataTextResourcePath ) ) ;
93+ var requestContent = TestHelpers . ReadEmbeddedFile ( requestDataTextResourcePath ) ;
14694 var response = await server
14795 . CreateRequest ( uri . ToString ( ) )
14896 . And ( request =>
14997 {
150- request . Content = new StringContent ( requestContent , Encoding . UTF8 ,
151- "application/vnd.api+json" ) ;
98+ request . Content = new StringContent ( requestContent , Encoding . UTF8 , "application/vnd.api+json" ) ;
15299 } ) . SendAsync ( "PUT" ) ;
153- response . StatusCode . Should ( ) . Be ( HttpStatusCode . OK ) ;
154- var responseContent = await response . Content . ReadAsStringAsync ( ) ;
155-
156- var expected =
157- JsonHelpers . MinifyJson ( TestHelpers . ReadEmbeddedFile ( expectedResponseTextResourcePath ) ) ;
158- responseContent . Should ( ) . Be ( expected ) ;
100+ return response ;
159101 }
160102 }
161103
162- protected async Task TestDelete ( DbConnection effortConnection , string requestPath )
104+ #endregion
105+ #region DELETE
106+
107+ protected async Task < HttpResponseMessage > SubmitDelete ( DbConnection effortConnection , string requestPath )
163108 {
164109 using ( var server = TestServer . Create ( app =>
165110 {
@@ -171,8 +116,10 @@ protected async Task TestDelete(DbConnection effortConnection, string requestPat
171116 var response = await server
172117 . CreateRequest ( uri . ToString ( ) )
173118 . SendAsync ( "DELETE" ) ;
174- response . StatusCode . Should ( ) . Be ( HttpStatusCode . NoContent ) ;
119+ return response ;
175120 }
176121 }
122+
123+ #endregion
177124 }
178125}
0 commit comments