예제 #1
0
        public async Task WithPerRequestAuthProvider_ShouldUsePerRequestAuthProviderAsync()
        {
            string authorizationHeader     = "PerRequest-Token";
            var    requestMockAuthProvider = new MockAuthenticationProvider(authorizationHeader);

            var baseRequest = new BaseRequest(requestUrl, baseClient);

            baseRequest.Client.PerRequestAuthProvider = () => requestMockAuthProvider.Object;
            baseRequest.WithPerRequestAuthProvider();

            using (var httpResponseMessage = new HttpResponseMessage())
            {
                var httpRequestMessage = baseRequest.GetHttpRequestMessage();
                testHttpMessageHandler.AddResponseMapping(httpRequestMessage.RequestUri.ToString(), httpResponseMessage);

                var returnedResponseMessage = await httpProvider.SendAsync(httpRequestMessage);

                Assert.Equal(httpResponseMessage, returnedResponseMessage);
                Assert.Equal(authorizationHeader, returnedResponseMessage.RequestMessage.Headers.Authorization.Parameter);
            }
        }
예제 #2
0
        public async Task SendAsync_WithCustomHttpProvider()
        {
            using (var httpResponseMessage = new HttpResponseMessage())
                using (TestHttpMessageHandler testHttpMessageHandler = new TestHttpMessageHandler())
                {
                    string requestUrl = "https://localhost/";
                    testHttpMessageHandler.AddResponseMapping(requestUrl, httpResponseMessage);
                    MockCustomHttpProvider customHttpProvider = new MockCustomHttpProvider(testHttpMessageHandler);

                    BaseClient  client      = new BaseClient(requestUrl, authenticationProvider.Object, customHttpProvider);
                    BaseRequest baseRequest = new BaseRequest(requestUrl, client);

                    HttpResponseMessage returnedResponse = await baseRequest.SendRequestAsync("string", CancellationToken.None);

                    Assert.Equal(httpResponseMessage, returnedResponse);
                    Assert.NotNull(returnedResponse.RequestMessage.Headers);
                    Assert.Equal("Default-Token", returnedResponse.RequestMessage.Headers.Authorization.Parameter);
                }
        }
        public async Task PutAsyncReturnsExpectedUploadSessionAsync()
        {
            using (HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.OK))
                using (TestHttpMessageHandler testHttpMessageHandler = new TestHttpMessageHandler())
                {
                    /* Arrange */
                    // 1. create a mock response
                    string      requestUrl   = "https://localhost/";
                    string      responseJSON = @"{
                  ""expirationDateTime"": ""2015 - 01 - 29T09: 21:55.523Z"",
                  ""nextExpectedRanges"": [
                  ""12345-55232"",
                  ""77829-99375""
                  ]
                }";
                    HttpContent content      = new StringContent(responseJSON, Encoding.UTF8, "application/json");
                    responseMessage.Content = content;

                    // 2. Map the response
                    testHttpMessageHandler.AddResponseMapping(requestUrl, responseMessage);

                    // 3. Create a batch request object to be tested
                    MockCustomHttpProvider         customHttpProvider = new MockCustomHttpProvider(testHttpMessageHandler);
                    BaseClient                     client             = new BaseClient(requestUrl, authenticationProvider.Object, customHttpProvider);
                    UploadSliceRequest <DriveItem> uploadSliceRequest = new UploadSliceRequest <DriveItem>(requestUrl, client, 0, 200, 1000);
                    Stream stream = new MemoryStream(new byte[300]);

                    /* Act */
                    var uploadResult = await uploadSliceRequest.PutAsync(stream);

                    var uploadSession = uploadResult.UploadSession;

                    /* Assert */
                    Assert.False(uploadResult.UploadSucceeded);
                    Assert.NotNull(uploadSession);
                    Assert.Null(uploadSession.UploadUrl);
                    Assert.Equal(DateTimeOffset.Parse("2015 - 01 - 29T09: 21:55.523Z"), uploadSession.ExpirationDateTime);
                    Assert.Equal("12345-55232", uploadSession.NextExpectedRanges.First());
                    Assert.Equal("77829-99375", uploadSession.NextExpectedRanges.Last());
                    Assert.Equal(2, uploadSession.NextExpectedRanges.Count());
                }
        }
예제 #4
0
        public async Task PostAsyncReturnsBatchResponseContent()
        {
            using (HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.OK))
                using (TestHttpMessageHandler testHttpMessageHandler = new TestHttpMessageHandler())
                {
                    /* Arrange */
                    // 1. create a mock response
                    string requestUrl   = "https://localhost/";
                    string responseJSON = "{\"responses\":"
                                          + "[{"
                                          + "\"id\": \"1\","
                                          + "\"status\":200,"
                                          + "\"headers\":{\"Cache-Control\":\"no-cache\",\"OData-Version\":\"4.0\",\"Content-Type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\"},"
                                          + "\"body\":{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#users/$entity\",\"displayName\":\"MOD Administrator\",\"jobTitle\":null,\"id\":\"9f4fe8ea-7e6e-486e-b8f4-VkHdanfIomf\"}"
                                          + "},"
                                          + "{"
                                          + "\"id\": \"2\","
                                          + "\"status\":409,"
                                          + "\"headers\" : {\"Cache-Control\":\"no-cache\"},"
                                          + "\"body\":{\"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\"}}}"
                                          + "}]}";
                    HttpContent content = new StringContent(responseJSON);
                    responseMessage.Content = content;

                    // 2. Map the response
                    testHttpMessageHandler.AddResponseMapping(requestUrl, responseMessage);

                    // 3. Create a batch request object to be tested
                    MockCustomHttpProvider customHttpProvider = new MockCustomHttpProvider(testHttpMessageHandler);
                    BaseClient             client             = new BaseClient(requestUrl, authenticationProvider.Object, customHttpProvider);
                    BatchRequest           batchRequest       = new BatchRequest(requestUrl, client);

                    // 4. Create batch request content to be sent out
                    // 4.1 Create HttpRequestMessages for the content
                    HttpRequestMessage httpRequestMessage1 = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me/");
                    HttpRequestMessage httpRequestMessage2 = new HttpRequestMessage(HttpMethod.Post, "https://graph.microsoft.com/v1.0/me/onenote/notebooks");

                    // 4.2 Create batch request steps with request ids.
                    BatchRequestStep requestStep1 = new BatchRequestStep("1", httpRequestMessage1);
                    BatchRequestStep requestStep2 = new BatchRequestStep("2", httpRequestMessage2, new List <string> {
                        "1"
                    });

                    // 4.3 Add batch request steps to BatchRequestContent.
                    BatchRequestContent batchRequestContent = new BatchRequestContent(requestStep1, requestStep2);

                    /* Act */
                    BatchResponseContent returnedResponse = await batchRequest.PostAsync(batchRequestContent);

                    HttpResponseMessage firstResponse = await returnedResponse.GetResponseByIdAsync("1");

                    HttpResponseMessage secondResponse = await returnedResponse.GetResponseByIdAsync("2");

                    /* Assert */
                    // validate the first response
                    Assert.NotNull(firstResponse);
                    Assert.Equal(HttpStatusCode.OK, firstResponse.StatusCode);
                    Assert.True(firstResponse.Headers.CacheControl.NoCache);
                    Assert.NotNull(firstResponse.Content);

                    // validate the second response
                    Assert.NotNull(secondResponse);
                    Assert.Equal(HttpStatusCode.Conflict, secondResponse.StatusCode);
                    Assert.True(secondResponse.Headers.CacheControl.NoCache);
                    Assert.NotNull(secondResponse.Content);
                }
        }