-
Notifications
You must be signed in to change notification settings - Fork 243
/
EntityRequestTests.cs
235 lines (203 loc) · 11.5 KB
/
EntityRequestTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// ------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------
using Microsoft.Graph;
using Moq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.Graph.DotnetCore.Test.Requests.Generated
{
public class EntityRequestTests : RequestTestBase
{
[Fact]
public async Task GetAsync_InitializeCollectionProperties()
{
using (var httpResponseMessage = new HttpResponseMessage())
using (var responseStream = new MemoryStream())
using (var streamContent = new StreamContent(responseStream))
{
httpResponseMessage.Content = streamContent;
var requestUrl = string.Format(Constants.Url.GraphBaseUrlFormatString, "v1.0") + "/me/drive/items/id";
this.httpProvider.Setup(
provider => provider.SendAsync(
It.Is<HttpRequestMessage>(
request => request.RequestUri.ToString().Equals(requestUrl)),
HttpCompletionOption.ResponseContentRead,
CancellationToken.None))
.Returns(System.Threading.Tasks.Task.FromResult<HttpResponseMessage>(httpResponseMessage));
var expectedChildrenPage = new DriveItemChildrenCollectionPage
{
new DriveItem { Id = "id" }
};
var expectedItemResponse = new DriveItem
{
AdditionalData = new Dictionary<string, object>
{
{ "children@odata.nextLink", requestUrl + "/next" }
},
Children = expectedChildrenPage,
};
this.serializer.Setup(
serializer => serializer.DeserializeObject<DriveItem>(It.IsAny<string>()))
.Returns(expectedItemResponse);
var item = await this.graphServiceClient.Me.Drive.Items["id"].Request().GetAsync();
Assert.NotNull(item);
Assert.NotNull(item.Children);
Assert.Equal(1, item.Children.CurrentPage.Count);
Assert.Equal("id", item.Children.CurrentPage[0].Id);
Assert.Equal(expectedItemResponse.AdditionalData, item.Children.AdditionalData);
var nextPageRequest = item.Children.NextPageRequest as DriveItemChildrenCollectionRequest;
Assert.NotNull(nextPageRequest);
Assert.Equal(new Uri(requestUrl + "/next"), new Uri(nextPageRequest.RequestUrl));
}
}
[Fact]
public async Task DeleteAsync()
{
using (var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.NoContent))
{
var requestUrl = string.Format(Constants.Url.GraphBaseUrlFormatString, "v1.0") + "/me/drive/items/id";
this.httpProvider.Setup(
provider => provider.SendAsync(
It.Is<HttpRequestMessage>(
request =>
request.Method == HttpMethod.Delete
&& request.RequestUri.ToString().Equals(requestUrl)),
HttpCompletionOption.ResponseContentRead,
CancellationToken.None))
.Returns(System.Threading.Tasks.Task.FromResult(httpResponseMessage));
await this.graphServiceClient.Me.Drive.Items["id"].Request().DeleteAsync();
}
}
[Fact]
public void Expand()
{
var expectedRequestUri = new Uri(string.Format(Constants.Url.GraphBaseUrlFormatString, "v1.0") + "/me/drive/items/id");
var itemRequest = this.graphServiceClient.Me.Drive.Items["id"].Request().Expand("value") as DriveItemRequest;
Assert.NotNull(itemRequest);
Assert.Equal(expectedRequestUri, new Uri(itemRequest.RequestUrl));
Assert.Equal(1, itemRequest.QueryOptions.Count);
Assert.Equal("$expand", itemRequest.QueryOptions[0].Name);
Assert.Equal("value", itemRequest.QueryOptions[0].Value);
}
[Fact]
public void Select()
{
var expectedRequestUri = new Uri(string.Format(Constants.Url.GraphBaseUrlFormatString, "v1.0") + "/me/drive/items/id");
var itemRequest = this.graphServiceClient.Me.Drive.Items["id"].Request().Select("value") as DriveItemRequest;
Assert.NotNull(itemRequest);
Assert.Equal(expectedRequestUri, new Uri(itemRequest.RequestUrl));
Assert.Equal(1, itemRequest.QueryOptions.Count);
Assert.Equal("$select", itemRequest.QueryOptions[0].Name);
Assert.Equal("value", itemRequest.QueryOptions[0].Value);
}
[Fact]
public async Task UpdateAsync_EntityWithNoCollecitonProperties()
{
using (var httpResponseMessage = new HttpResponseMessage())
using (var responseStream = new MemoryStream())
using (var streamContent = new StreamContent(responseStream))
{
httpResponseMessage.Content = streamContent;
var requestUrl = string.Format(Constants.Url.GraphBaseUrlFormatString, "v1.0") + "/me/contacts/id";
this.httpProvider.Setup(
provider => provider.SendAsync(
It.Is<HttpRequestMessage>(
request =>
string.Equals(request.Method.ToString().ToUpperInvariant(), "PATCH")
&& string.Equals(request.Content.Headers.ContentType.ToString(), "application/json")
&& request.RequestUri.ToString().Equals(requestUrl)),
HttpCompletionOption.ResponseContentRead,
CancellationToken.None))
.Returns(System.Threading.Tasks.Task.FromResult(httpResponseMessage));
var contactToUpdate = new Contact { Id = "id" };
this.serializer.Setup(serializer => serializer.SerializeObject(contactToUpdate)).Returns("body");
this.serializer.Setup(serializer => serializer.DeserializeObject<Contact>(It.IsAny<string>())).Returns(contactToUpdate);
var contactResponse = await this.graphServiceClient.Me.Contacts["id"].Request().UpdateAsync(contactToUpdate);
Assert.Equal(contactToUpdate, contactResponse);
}
}
[Fact]
public async Task UpdateAsync()
{
await this.RequestWithItemInBody(true);
}
private async Task RequestWithItemInBody(bool isUpdate)
{
using (var httpResponseMessage = new HttpResponseMessage())
using (var responseStream = new MemoryStream())
using (var streamContent = new StreamContent(responseStream))
{
httpResponseMessage.Content = streamContent;
var requestUrl = string.Format(Constants.Url.GraphBaseUrlFormatString, "v1.0") + "/me/drive/items/id";
this.httpProvider.Setup(
provider => provider.SendAsync(
It.Is<HttpRequestMessage>(
request =>
string.Equals(request.Method.ToString().ToUpperInvariant(), isUpdate ? "PATCH" : "PUT")
&& string.Equals(request.Content.Headers.ContentType.ToString(), "application/json")
&& request.RequestUri.ToString().Equals(requestUrl)),
HttpCompletionOption.ResponseContentRead,
CancellationToken.None))
.Returns(System.Threading.Tasks.Task.FromResult(httpResponseMessage));
this.serializer.Setup(serializer => serializer.SerializeObject(It.IsAny<DriveItem>())).Returns("body");
this.serializer.Setup(serializer => serializer.DeserializeObject<DriveItem>(It.IsAny<string>())).Returns(new DriveItem { Id = "id" });
var itemResponse = isUpdate
? await this.graphServiceClient.Me.Drive.Items["id"].Request().UpdateAsync(new DriveItem())
: await this.graphServiceClient.Me.Drive.Items["id"].Request().CreateAsync(new DriveItem());
Assert.Equal("id", itemResponse.Id);
}
}
[Fact]
public async System.Threading.Tasks.Task UpdateAsync_EntityWithResponseHeadersAndStatusCode()
{
using (var httpResponseMessage = new HttpResponseMessage())
using (var responseStream = new MemoryStream())
using (var streamContent = new StreamContent(responseStream))
{
httpResponseMessage.Content = streamContent;
var requestUrl = string.Format(Constants.Url.GraphBaseUrlFormatString, "v1.0") + "/me/contacts/id";
this.httpProvider.Setup(
provider => provider.SendAsync(
It.Is<HttpRequestMessage>(
request =>
string.Equals(request.Method.ToString().ToUpperInvariant(), "PATCH")
&& string.Equals(request.Content.Headers.ContentType.ToString(), "application/json")
&& request.RequestUri.ToString().Equals(requestUrl)),
HttpCompletionOption.ResponseContentRead,
CancellationToken.None))
.Returns(System.Threading.Tasks.Task.FromResult(httpResponseMessage));
var contactToUpdate = new Contact
{
Id = "id",
AdditionalData = new Dictionary<string, object>
{
{ Constants.HttpPropertyNames.ResponseHeaders, new { Value = "Response headers test value" } },
{ Constants.HttpPropertyNames.StatusCode, new { Value = "Status code test value" }}
}
};
this.serializer.Setup(serializer => serializer.SerializeObject(contactToUpdate)).Returns("body");
this.serializer.Setup(serializer => serializer.DeserializeObject<Contact>(It.IsAny<string>())).Returns(contactToUpdate);
try
{
await Assert.ThrowsAsync<ClientException>(async () => await this.graphServiceClient.Me.Contacts["id"].Request().UpdateAsync(contactToUpdate));
}
catch (ClientException exception)
{
string expectedCode = "notAllowed";
string expectedMessage = "Do not use objects returned in a response for updating an object in Microsoft Graph. " +
$"Create a new {contactToUpdate.GetType().Name} object and only set the updated properties on it.";
Assert.Equal(expectedCode, exception.Error.Code);
Assert.Equal(expectedMessage, exception.Error.Message);
}
}
}
}
}