9
9
using System . Net ;
10
10
using JsonApiDotNetCoreExample . Models ;
11
11
using JsonApiDotNetCoreExample . Data ;
12
+ using System ;
13
+ using Newtonsoft . Json ;
14
+ using System . Net . Http . Headers ;
12
15
13
16
namespace NoEntityFrameworkTests . Acceptance . Extensibility
14
17
{
@@ -27,7 +30,7 @@ public NoEntityFrameworkTests()
27
30
}
28
31
29
32
[ Fact ]
30
- public async Task Can_Implement_Custom_IResourceService_Without_EFAsync ( )
33
+ public async Task Can_Get_TodoItems ( )
31
34
{
32
35
// arrange
33
36
_context . TodoItems . Add ( new TodoItem ( ) ) ;
@@ -51,5 +54,69 @@ public async Task Can_Implement_Custom_IResourceService_Without_EFAsync()
51
54
Assert . NotNull ( deserializedBody ) ;
52
55
Assert . NotEmpty ( deserializedBody ) ;
53
56
}
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
+ }
54
121
}
55
122
}
0 commit comments