public async Task RestClientGetRequestReturnsResponse() { var tc = new TestContext(); var client = tc.GetRestClient(); var anyRequest = new RestRequest { Method = Method.GET, Resource = AnyResource, RootElement = AnyRootElement }; var successfulResponseMessage = CreateSuccessfulResponseMessage(); tc.HttpClient.GetAsync(AnyRequestUri + anyRequest.Resource, CancellationToken.None) .Returns(Task.FromResult(successfulResponseMessage)); var response = await client.ExecuteRequestAsync<AnyResponse>(anyRequest, AnyRequestUri, CancellationToken.None); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(AnyStringField, response.Data.AnyStringField); Assert.Equal(AnyIntField, response.Data.AnyIntField); Assert.Null(response.Exception); var unused = tc.HttpClient.Received(1).GetAsync(AnyRequestUri + anyRequest.Resource, CancellationToken.None); }
/// <summary> /// Generate REST request /// </summary> public RestRequest CreateRestRequest(string resourcePath, Method method, string rootElement, object body) { var request = new RestRequest { Resource = _baseResourceString + resourcePath, RootElement = rootElement, Method = method, }; if (body != null) { string content = _serializer.Serialize(body); request.Content = new StringContent(content, Encoding.UTF8, @"application/json"); } return request; }
private static bool IsExpectedJson(RestRequest req, string expectedJson) { return req.Content.ReadAsStringAsync().Result == expectedJson; }
public async Task RestClientPostRequestReturnsResponse() { var tc = new TestContext(); var client = tc.GetRestClient(); var anyRequest = new RestRequest { Method = Method.POST, Resource = AnyResource, RootElement = AnyRootElement, Content = new StringContent(AnyPostContent, Encoding, MediaType) }; var successfulResponseMessage = CreateSuccessfulResponseMessage(); tc.HttpClient.PostAsync(AnyRequestUri + anyRequest.Resource, Arg.Is<StringContent>( stringContent => stringContent.Headers.ContentType.MediaType == MediaType && stringContent.ReadAsStringAsync().Result == AnyPostContent), CancellationToken.None) .Returns(Task.FromResult(successfulResponseMessage)); var response = await client.ExecuteRequestAsync<AnyResponse>(anyRequest, AnyRequestUri, CancellationToken.None); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(AnyStringField, response.Data.AnyStringField); Assert.Equal(AnyIntField, response.Data.AnyIntField); Assert.Null(response.Exception); var unused = tc.HttpClient.Received(1).PostAsync(AnyRequestUri + anyRequest.Resource, Arg.Is<StringContent>( stringContent => stringContent.Headers.ContentType.MediaType == MediaType && stringContent.ReadAsStringAsync().Result == AnyPostContent), CancellationToken.None); }
public async Task RestClientRequestReturnsFailureResponse() { var tc = new TestContext(); var client = tc.GetRestClient(); var anyRequest = new RestRequest { Method = Method.GET, Resource = AnyResource, RootElement = AnyRootElement }; var successfulResponseMessage = CreateFailedResponseMessage(); tc.HttpClient.GetAsync(AnyRequestUri + anyRequest.Resource, CancellationToken.None) .Returns(Task.FromResult(successfulResponseMessage)); var response = await client.ExecuteRequestAsync<AnyResponse>(anyRequest, AnyRequestUri, CancellationToken.None); Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); Assert.Null(response.Data); Assert.NotNull(response.Exception); Assert.IsType(typeof(HttpRequestException), response.Exception); var unused = tc.HttpClient.Received(1).GetAsync(AnyRequestUri + anyRequest.Resource, CancellationToken.None); }