@@ -6,10 +6,15 @@ namespace Microsoft.Graph.DotnetCore.Core.Test.Requests
6
6
{
7
7
using System . Collections . Generic ;
8
8
using System . Net . Http ;
9
+ using System . Text ;
10
+ using System . Threading ;
9
11
using System . Threading . Tasks ;
10
12
using Microsoft . Graph . Core . Requests ;
11
13
using Microsoft . Graph . DotnetCore . Core . Test . Mocks ;
14
+ using Microsoft . Kiota . Abstractions ;
12
15
using Microsoft . Kiota . Abstractions . Authentication ;
16
+ using Microsoft . Kiota . Abstractions . Serialization ;
17
+ using Moq ;
13
18
using Xunit ;
14
19
15
20
public class BatchRequestBuilderTests
@@ -42,5 +47,119 @@ public async Task BatchRequestBuilderAsync()
42
47
Assert . Equal ( "{+baseurl}/$batch" , requestInformation . UrlTemplate ) ;
43
48
Assert . Equal ( baseClient . RequestAdapter , batchRequestBuilder . RequestAdapter ) ;
44
49
}
50
+
51
+
52
+ [ Fact ]
53
+ public async Task BatchRequestBuilderPostAsyncHandlesDoesNotThrowExceptionAsync ( )
54
+ {
55
+ // Arrange
56
+ var requestAdapter = new Mock < IRequestAdapter > ( ) ;
57
+ IBaseClient baseClient = new BaseClient ( requestAdapter . Object ) ;
58
+
59
+ var errorResponseMessage = new HttpResponseMessage ( System . Net . HttpStatusCode . OK )
60
+ {
61
+ Content = new StringContent ( "{}" , Encoding . UTF8 , "application/json" ) , //dummy content
62
+ } ;
63
+ requestAdapter
64
+ . Setup ( requestAdapter => requestAdapter . SendNoContentAsync ( It . IsAny < RequestInformation > ( ) , It . IsAny < Dictionary < string , ParsableFactory < IParsable > > > ( ) , It . IsAny < CancellationToken > ( ) ) )
65
+ . Callback ( ( RequestInformation requestInfo , Dictionary < string , ParsableFactory < IParsable > > errorMapping , CancellationToken cancellationToken ) => ( ( NativeResponseHandler ) requestInfo . GetRequestOption < ResponseHandlerOption > ( ) . ResponseHandler ) . Value = errorResponseMessage )
66
+ . Returns ( Task . FromResult ( 0 ) ) ;
67
+
68
+ // Act
69
+ var batchRequestBuilder = new BatchRequestBuilder ( baseClient . RequestAdapter ) ;
70
+
71
+ // 4. Create batch request content to be sent out
72
+ // 4.1 Create HttpRequestMessages for the content
73
+ HttpRequestMessage httpRequestMessage1 = new HttpRequestMessage ( HttpMethod . Get , "https://graph.microsoft.com/v1.0/me/" ) ;
74
+
75
+ // 4.2 Create batch request steps with request ids.
76
+ BatchRequestStep requestStep1 = new BatchRequestStep ( "1" , httpRequestMessage1 ) ;
77
+
78
+ // 4.3 Add batch request steps to BatchRequestContent.
79
+ #pragma warning disable CS0618 // Type or member is obsolete use the BatchRequestContentCollection for making batch requests
80
+ BatchRequestContent batchRequestContent = new BatchRequestContent ( baseClient , requestStep1 ) ;
81
+ #pragma warning restore CS0618 // Type or member is obsolete use the BatchRequestContentCollection for making batch requests
82
+ var responseContent = await batchRequestBuilder . PostAsync ( batchRequestContent ) ;
83
+
84
+ // Assert
85
+ Assert . NotNull ( responseContent ) ;
86
+ }
87
+
88
+ [ Fact ]
89
+ public async Task BatchRequestBuilderPostAsyncHandlesNonSuccessStatusWithJsonResponseAsync ( )
90
+ {
91
+ // Arrange
92
+ var requestAdapter = new Mock < IRequestAdapter > ( ) ;
93
+ IBaseClient baseClient = new BaseClient ( requestAdapter . Object ) ;
94
+
95
+ var errorResponseMessage = new HttpResponseMessage ( System . Net . HttpStatusCode . Unauthorized )
96
+ {
97
+ Content = new StringContent ( "{\" error\" : {\" code\" : \" 20117\" ,\" message\" : \" An item with this name already exists in this location.\" ,\" innerError\" :{\" request-id\" : \" nothing1b13-45cd-new-92be873c5781\" ,\" date\" : \" 2019-03-22T23:17:50\" }}}" , Encoding . UTF8 , "application/json" ) ,
98
+ } ;
99
+ requestAdapter
100
+ . Setup ( requestAdapter => requestAdapter . SendNoContentAsync ( It . IsAny < RequestInformation > ( ) , It . IsAny < Dictionary < string , ParsableFactory < IParsable > > > ( ) , It . IsAny < CancellationToken > ( ) ) )
101
+ . Callback ( ( RequestInformation requestInfo , Dictionary < string , ParsableFactory < IParsable > > errorMapping , CancellationToken cancellationToken ) => ( ( NativeResponseHandler ) requestInfo . GetRequestOption < ResponseHandlerOption > ( ) . ResponseHandler ) . Value = errorResponseMessage )
102
+ . Returns ( Task . FromResult ( 0 ) ) ;
103
+
104
+ // Act
105
+ var batchRequestBuilder = new BatchRequestBuilder ( baseClient . RequestAdapter ) ;
106
+
107
+ // 4. Create batch request content to be sent out
108
+ // 4.1 Create HttpRequestMessages for the content
109
+ HttpRequestMessage httpRequestMessage1 = new HttpRequestMessage ( HttpMethod . Get , "https://graph.microsoft.com/v1.0/me/" ) ;
110
+
111
+ // 4.2 Create batch request steps with request ids.
112
+ BatchRequestStep requestStep1 = new BatchRequestStep ( "1" , httpRequestMessage1 ) ;
113
+
114
+ // 4.3 Add batch request steps to BatchRequestContent.
115
+ #pragma warning disable CS0618 // Type or member is obsolete use the BatchRequestContentCollection for making batch requests
116
+ BatchRequestContent batchRequestContent = new BatchRequestContent ( baseClient , requestStep1 ) ;
117
+ #pragma warning restore CS0618 // Type or member is obsolete use the BatchRequestContentCollection for making batch requests
118
+ var serviceException = await Assert . ThrowsAsync < ServiceException > ( async ( ) => await batchRequestBuilder . PostAsync ( batchRequestContent ) ) ;
119
+
120
+ // Assert
121
+ Assert . Equal ( ErrorConstants . Messages . BatchRequestError , serviceException . Message ) ;
122
+ Assert . Equal ( 401 , serviceException . ResponseStatusCode ) ;
123
+ Assert . NotNull ( serviceException . InnerException ) ;
124
+ Assert . Equal ( "20117 : An item with this name already exists in this location." , serviceException . InnerException . Message ) ;
125
+ }
126
+
127
+ [ Fact ]
128
+ public async Task BatchRequestBuilderPostAsyncHandlesNonSuccessStatusWithNonJsonResponseAsync ( )
129
+ {
130
+ // Arrange
131
+ var requestAdapter = new Mock < IRequestAdapter > ( ) ;
132
+ IBaseClient baseClient = new BaseClient ( requestAdapter . Object ) ;
133
+
134
+ var errorResponseMessage = new HttpResponseMessage ( System . Net . HttpStatusCode . Conflict )
135
+ {
136
+ Content = new StringContent ( "<html>This is random html</html>" , Encoding . UTF8 , "text/plain" ) ,
137
+ } ;
138
+ requestAdapter
139
+ . Setup ( requestAdapter => requestAdapter . SendNoContentAsync ( It . IsAny < RequestInformation > ( ) , It . IsAny < Dictionary < string , ParsableFactory < IParsable > > > ( ) , It . IsAny < CancellationToken > ( ) ) )
140
+ . Callback ( ( RequestInformation requestInfo , Dictionary < string , ParsableFactory < IParsable > > errorMapping , CancellationToken cancellationToken ) => ( ( NativeResponseHandler ) requestInfo . GetRequestOption < ResponseHandlerOption > ( ) . ResponseHandler ) . Value = errorResponseMessage )
141
+ . Returns ( Task . FromResult ( 0 ) ) ;
142
+
143
+ // Act
144
+ var batchRequestBuilder = new BatchRequestBuilder ( baseClient . RequestAdapter ) ;
145
+
146
+ // 4. Create batch request content to be sent out
147
+ // 4.1 Create HttpRequestMessages for the content
148
+ HttpRequestMessage httpRequestMessage1 = new HttpRequestMessage ( HttpMethod . Get , "https://graph.microsoft.com/v1.0/me/" ) ;
149
+
150
+ // 4.2 Create batch request steps with request ids.
151
+ BatchRequestStep requestStep1 = new BatchRequestStep ( "1" , httpRequestMessage1 ) ;
152
+
153
+ // 4.3 Add batch request steps to BatchRequestContent.
154
+ #pragma warning disable CS0618 // Type or member is obsolete use the BatchRequestContentCollection for making batch requests
155
+ BatchRequestContent batchRequestContent = new BatchRequestContent ( baseClient , requestStep1 ) ;
156
+ #pragma warning restore CS0618 // Type or member is obsolete use the BatchRequestContentCollection for making batch requests
157
+ var serviceException = await Assert . ThrowsAsync < ServiceException > ( async ( ) => await batchRequestBuilder . PostAsync ( batchRequestContent ) ) ;
158
+
159
+ // Assert
160
+ Assert . Equal ( ErrorConstants . Messages . BatchRequestError , serviceException . Message ) ;
161
+ Assert . Equal ( 409 , serviceException . ResponseStatusCode ) ;
162
+ Assert . Equal ( "<html>This is random html</html>" , serviceException . RawResponseBody ) ;
163
+ }
45
164
}
46
165
}
0 commit comments