99using System . Net ;
1010using JsonApiDotNetCoreExample . Models ;
1111using JsonApiDotNetCoreExample . Data ;
12+ using System ;
13+ using Newtonsoft . Json ;
14+ using System . Net . Http . Headers ;
1215
1316namespace NoEntityFrameworkTests . Acceptance . Extensibility
1417{
@@ -27,7 +30,7 @@ public NoEntityFrameworkTests()
2730 }
2831
2932 [ Fact ]
30- public async Task Can_Implement_Custom_IResourceService_Without_EFAsync ( )
33+ public async Task Can_Get_TodoItems ( )
3134 {
3235 // arrange
3336 _context . TodoItems . Add ( new TodoItem ( ) ) ;
@@ -51,5 +54,69 @@ public async Task Can_Implement_Custom_IResourceService_Without_EFAsync()
5154 Assert . NotNull ( deserializedBody ) ;
5255 Assert . NotEmpty ( deserializedBody ) ;
5356 }
57+
58+ [ Fact ]
59+ public async Task Can_Get_TodoItems_By_Id ( )
60+ {
61+ // arrange
62+ var todoItem = new TodoItem ( ) ;
63+ _context . TodoItems . Add ( todoItem ) ;
64+ _context . SaveChanges ( ) ;
65+
66+ var client = _server . CreateClient ( ) ;
67+
68+ var httpMethod = new HttpMethod ( "GET" ) ;
69+ var route = $ "/api/v1/custom-todo-items/{ todoItem . Id } ";
70+
71+ var request = new HttpRequestMessage ( httpMethod , route ) ;
72+
73+ // act
74+ var response = await client . SendAsync ( request ) ;
75+ var responseBody = await response . Content . ReadAsStringAsync ( ) ;
76+ var deserializedBody = ( TodoItem ) _server . GetService < IJsonApiDeSerializer > ( )
77+ . Deserialize ( responseBody ) ;
78+
79+ // assert
80+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
81+ Assert . NotNull ( deserializedBody ) ;
82+ Assert . Equal ( todoItem . Id , deserializedBody . Id ) ;
83+ }
84+
85+ [ Fact ]
86+ public async Task Can_Create_TodoItems ( )
87+ {
88+ // arrange
89+ var description = Guid . NewGuid ( ) . ToString ( ) ;
90+ var client = _server . CreateClient ( ) ;
91+ var httpMethod = new HttpMethod ( "POST" ) ;
92+ var route = $ "/api/v1/custom-todo-items/";
93+ var content = new
94+ {
95+ data = new
96+ {
97+ type = "custom-todo-items" ,
98+ attributes = new
99+ {
100+ description = description ,
101+ ordinal = 1
102+ }
103+ }
104+ } ;
105+
106+ var request = new HttpRequestMessage ( httpMethod , route ) ;
107+ request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
108+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
109+
110+ // act
111+ var response = await client . SendAsync ( request ) ;
112+ var responseBody = await response . Content . ReadAsStringAsync ( ) ;
113+ var deserializedBody = ( TodoItem ) _server . GetService < IJsonApiDeSerializer > ( )
114+ . Deserialize ( responseBody ) ;
115+
116+ // assert
117+ Assert . Equal ( HttpStatusCode . Created , response . StatusCode ) ;
118+ Assert . NotNull ( deserializedBody ) ;
119+ Assert . Equal ( description , deserializedBody . Description ) ;
120+ }
54121 }
55122}
0 commit comments