@@ -4,57 +4,91 @@ public class PetTest
44{
55 private const string _baseAddress = "https://mockdomain.mock" ;
66
7- [ Fact ]
8- public async void GivenPetObjectHasValues_WhenPostAsStringContentIsCalled_ThenPetResultIsReturned ( )
7+ private static ( HttpClient Client , List < string ? > ContentTypes ) CreateClient ( )
98 {
9+ var contentTypes = new List < string ? > ( ) ;
1010 var httpMessageHandlerMock = new Mock < HttpMessageHandler > ( ) ;
1111
1212 httpMessageHandlerMock . Protected ( )
1313 . Setup < Task < HttpResponseMessage > > ( "SendAsync" , ItExpr . Is < HttpRequestMessage > ( r => r . RequestUri ! . AbsoluteUri == $ "{ _baseAddress } /pet") , ItExpr . IsAny < CancellationToken > ( ) )
14- . ReturnsAsync ( new HttpResponseMessage
14+ . Callback < HttpRequestMessage , CancellationToken > ( ( request , _ ) => contentTypes . Add ( request . Content ? . Headers . ContentType ? . ToString ( ) ) )
15+ . ReturnsAsync ( ( ) => new HttpResponseMessage
1516 {
1617 StatusCode = HttpStatusCode . OK ,
17- Content = new StringContent ( "{\n \" id\" : 12,\n \" name\" : \" German Shepherd\" \n }" )
18+ Content = new StringContent ( "{\n \" id\" : 12,\n \" name\" : \" German Shepherd\" \n }" , Encoding . UTF8 , "application/json" )
1819 } ) ;
1920
20- var httpClientMock = new HttpClient ( httpMessageHandlerMock . Object )
21+ var httpClient = new HttpClient ( httpMessageHandlerMock . Object )
2122 {
2223 BaseAddress = new Uri ( _baseAddress )
2324 } ;
2425
25- var successResult = await new PetService ( httpClientMock )
26+ return ( httpClient , contentTypes ) ;
27+ }
28+
29+ [ Fact ]
30+ public async Task GivenPetObjectHasValues_WhenPostAsStringContentIsCalled_ThenPetResultIsReturned ( )
31+ {
32+ var ( httpClient , contentTypes ) = CreateClient ( ) ;
33+
34+ var successResult = await new PetService ( httpClient )
2635 . PostAsStringContentAsync ( ) ;
2736
2837 Assert . NotNull ( successResult ) ;
2938 Assert . Equal ( 12 , successResult ! . Id ) ;
30-
3139 Assert . Equal ( "German Shepherd" , successResult ! . Name ) ;
40+ Assert . Equal ( "application/json; charset=utf-8" , Assert . Single ( contentTypes ) ) ;
3241 }
3342
3443 [ Fact ]
35- public async void GivenPetObjectHasValues_WhenPostAsJsonIsCalled_ThenPetResultIsReturned ( )
44+ public async Task GivenPetObjectHasValues_WhenPostWithPostAsJsonIsCalled_ThenPetResultIsReturned ( )
3645 {
37- var httpMessageHandlerMock = new Mock < HttpMessageHandler > ( ) ;
46+ var ( httpClient , contentTypes ) = CreateClient ( ) ;
3847
39- httpMessageHandlerMock . Protected ( )
40- . Setup < Task < HttpResponseMessage > > ( "SendAsync" , ItExpr . Is < HttpRequestMessage > ( r => r . RequestUri ! . AbsoluteUri == $ "{ _baseAddress } /pet") , ItExpr . IsAny < CancellationToken > ( ) )
41- . ReturnsAsync ( new HttpResponseMessage
42- {
43- StatusCode = HttpStatusCode . OK ,
44- Content = new StringContent ( "{\n \" id\" : 12,\n \" name\" : \" German Shepherd\" \n }" )
45- } ) ;
48+ var successResult = await new PetService ( httpClient )
49+ . PostWithPostAsJsonAsync ( ) ;
4650
47- var httpClientMock = new HttpClient ( httpMessageHandlerMock . Object )
48- {
49- BaseAddress = new Uri ( _baseAddress )
50- } ;
51-
52- var successResult = await new PetService ( httpClientMock )
53- . PostAsJsonAsync ( ) ;
51+ Assert . NotNull ( successResult ) ;
52+ Assert . Equal ( 12 , successResult ! . Id ) ;
53+ Assert . Equal ( "German Shepherd" , successResult ! . Name ) ;
54+ Assert . Equal ( "application/json; charset=utf-8" , Assert . Single ( contentTypes ) ) ;
55+ }
56+
57+ [ Fact ]
58+ public async Task GivenPetObjectHasValues_WhenPostAsJsonContentIsCalled_ThenPetResultIsReturned ( )
59+ {
60+ var ( httpClient , contentTypes ) = CreateClient ( ) ;
61+
62+ var successResult = await new PetService ( httpClient )
63+ . PostAsJsonContentAsync ( ) ;
5464
5565 Assert . NotNull ( successResult ) ;
5666 Assert . Equal ( 12 , successResult ! . Id ) ;
67+ Assert . Equal ( "German Shepherd" , successResult ! . Name ) ;
68+ Assert . Equal ( "application/json; charset=utf-8" , Assert . Single ( contentTypes ) ) ;
69+ }
70+
71+ [ Fact ]
72+ public async Task GivenPetObjectHasValues_WhenPostAsSourceGeneratedJsonIsCalled_ThenPetResultIsReturned ( )
73+ {
74+ var ( httpClient , contentTypes ) = CreateClient ( ) ;
75+
76+ var successResult = await new PetService ( httpClient )
77+ . PostAsSourceGeneratedJsonAsync ( ) ;
5778
79+ Assert . NotNull ( successResult ) ;
80+ Assert . Equal ( 12 , successResult ! . Id ) ;
5881 Assert . Equal ( "German Shepherd" , successResult ! . Name ) ;
82+ Assert . Equal ( "application/json; charset=utf-8" , Assert . Single ( contentTypes ) ) ;
83+ }
84+
85+ [ Fact ]
86+ public void GivenStringContent_WhenMediaTypeIsOmitted_ThenContentTypeIsTextPlain ( )
87+ {
88+ var withoutMediaType = new StringContent ( "{}" , Encoding . UTF8 ) ;
89+ var withMediaType = new StringContent ( "{}" , Encoding . UTF8 , "application/json" ) ;
90+
91+ Assert . Equal ( "text/plain; charset=utf-8" , withoutMediaType . Headers . ContentType ! . ToString ( ) ) ;
92+ Assert . Equal ( "application/json; charset=utf-8" , withMediaType . Headers . ContentType ! . ToString ( ) ) ;
5993 }
6094}
0 commit comments