1
1
using System . Collections ;
2
2
using System . Net ;
3
3
using System . Net . Http ;
4
+ using System . Net . Http . Headers ;
4
5
using System . Threading . Tasks ;
5
6
using JsonApiDotNetCore . Models ;
6
7
using JsonApiDotNetCoreExample ;
@@ -47,13 +48,123 @@ public async Task Total_Record_Count_Included()
47
48
var response = await client . SendAsync ( request ) ;
48
49
var responseBody = await response . Content . ReadAsStringAsync ( ) ;
49
50
var documents = JsonConvert . DeserializeObject < Documents > ( responseBody ) ;
50
-
51
+
51
52
// assert
52
53
Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
53
54
Assert . NotNull ( documents . Meta ) ;
54
55
Assert . Equal ( ( long ) expectedCount , ( long ) documents . Meta [ "total-records" ] ) ;
55
56
}
56
57
58
+ [ Fact ]
59
+ public async Task Total_Record_Count_Included_When_None ( )
60
+ {
61
+ // arrange
62
+ _context . TodoItems . RemoveRange ( _context . TodoItems ) ;
63
+ _context . SaveChanges ( ) ;
64
+ var builder = new WebHostBuilder ( )
65
+ . UseStartup < MetaStartup > ( ) ;
66
+
67
+ var httpMethod = new HttpMethod ( "GET" ) ;
68
+ var route = $ "/api/v1/todo-items";
69
+
70
+ var server = new TestServer ( builder ) ;
71
+ var client = server . CreateClient ( ) ;
72
+ var request = new HttpRequestMessage ( httpMethod , route ) ;
73
+
74
+ // act
75
+ var response = await client . SendAsync ( request ) ;
76
+ var responseBody = await response . Content . ReadAsStringAsync ( ) ;
77
+ var documents = JsonConvert . DeserializeObject < Documents > ( responseBody ) ;
78
+
79
+ // assert
80
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
81
+ Assert . NotNull ( documents . Meta ) ;
82
+ Assert . Equal ( 0 , ( long ) documents . Meta [ "total-records" ] ) ;
83
+ }
84
+
85
+ [ Fact ]
86
+ public async Task Total_Record_Count_Not_Included_In_POST_Response ( )
87
+ {
88
+ // arrange
89
+ _context . TodoItems . RemoveRange ( _context . TodoItems ) ;
90
+ _context . SaveChanges ( ) ;
91
+ var builder = new WebHostBuilder ( )
92
+ . UseStartup < MetaStartup > ( ) ;
93
+
94
+ var httpMethod = new HttpMethod ( "POST" ) ;
95
+ var route = $ "/api/v1/todo-items";
96
+
97
+ var server = new TestServer ( builder ) ;
98
+ var client = server . CreateClient ( ) ;
99
+ var request = new HttpRequestMessage ( httpMethod , route ) ;
100
+ var content = new
101
+ {
102
+ data = new
103
+ {
104
+ type = "todo-items" ,
105
+ attributes = new
106
+ {
107
+ description = "New Description" ,
108
+ }
109
+ }
110
+ } ;
111
+
112
+ request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
113
+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
114
+
115
+ // act
116
+ var response = await client . SendAsync ( request ) ;
117
+ var responseBody = await response . Content . ReadAsStringAsync ( ) ;
118
+ var documents = JsonConvert . DeserializeObject < Document > ( responseBody ) ;
119
+
120
+ // assert
121
+ Assert . Equal ( HttpStatusCode . Created , response . StatusCode ) ;
122
+ Assert . False ( documents . Meta . ContainsKey ( "total-records" ) ) ;
123
+ }
124
+
125
+ [ Fact ]
126
+ public async Task Total_Record_Count_Not_Included_In_PATCH_Response ( )
127
+ {
128
+ // arrange
129
+ _context . TodoItems . RemoveRange ( _context . TodoItems ) ;
130
+ TodoItem todoItem = new TodoItem ( ) ;
131
+ _context . TodoItems . Add ( todoItem ) ;
132
+ _context . SaveChanges ( ) ;
133
+ var builder = new WebHostBuilder ( )
134
+ . UseStartup < MetaStartup > ( ) ;
135
+
136
+ var httpMethod = new HttpMethod ( "PATCH" ) ;
137
+ var route = $ "/api/v1/todo-items/{ todoItem . Id } ";
138
+
139
+ var server = new TestServer ( builder ) ;
140
+ var client = server . CreateClient ( ) ;
141
+ var request = new HttpRequestMessage ( httpMethod , route ) ;
142
+ var content = new
143
+ {
144
+ data = new
145
+ {
146
+ type = "todo-items" ,
147
+ id = todoItem . Id ,
148
+ attributes = new
149
+ {
150
+ description = "New Description" ,
151
+ }
152
+ }
153
+ } ;
154
+
155
+ request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
156
+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
157
+
158
+ // act
159
+ var response = await client . SendAsync ( request ) ;
160
+ var responseBody = await response . Content . ReadAsStringAsync ( ) ;
161
+ var documents = JsonConvert . DeserializeObject < Document > ( responseBody ) ;
162
+
163
+ // assert
164
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
165
+ Assert . False ( documents . Meta . ContainsKey ( "total-records" ) ) ;
166
+ }
167
+
57
168
[ Fact ]
58
169
public async Task EntityThatImplements_IHasMeta_Contains_MetaData ( )
59
170
{
@@ -73,26 +184,26 @@ public async Task EntityThatImplements_IHasMeta_Contains_MetaData()
73
184
// act
74
185
var response = await client . SendAsync ( request ) ;
75
186
var documents = JsonConvert . DeserializeObject < Documents > ( await response . Content . ReadAsStringAsync ( ) ) ;
76
-
187
+
77
188
// assert
78
189
Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
79
190
Assert . NotNull ( documents . Meta ) ;
80
191
Assert . NotNull ( expectedMeta ) ;
81
192
Assert . NotEmpty ( expectedMeta ) ;
82
-
83
- foreach ( var hash in expectedMeta )
193
+
194
+ foreach ( var hash in expectedMeta )
84
195
{
85
- if ( hash . Value is IList )
196
+ if ( hash . Value is IList )
86
197
{
87
198
var listValue = ( IList ) hash . Value ;
88
- for ( var i = 0 ; i < listValue . Count ; i ++ )
199
+ for ( var i = 0 ; i < listValue . Count ; i ++ )
89
200
Assert . Equal ( listValue [ i ] . ToString ( ) , ( ( IList ) documents . Meta [ hash . Key ] ) [ i ] . ToString ( ) ) ;
90
201
}
91
202
else
92
203
{
93
204
Assert . Equal ( hash . Value , documents . Meta [ hash . Key ] ) ;
94
205
}
95
- }
206
+ }
96
207
}
97
208
}
98
209
}
0 commit comments