public void Send_WithBody_Success() { var httpClientMock = new HttpClientMock(); var httpClient = new DefaultHttpClient(httpClientMock.HttpClient); var client = new DummyClient(httpClient, null); var path = "/x"; var url = $"{MercadoPagoConfig.BaseUrl}{path}"; var request = new DummyRequest("hello"); var httpResponse = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{'name':'world'}"), }; httpResponse.Headers.Add("X-Custom-Response-Header", "456"); // Mock httpClientMock.MockRequest(url, System.Net.Http.HttpMethod.Post, httpResponse); DummyResource dummyResource = client.Send(path, MercadoPagoCore.Http.HttpMethod.Post, request); Assert.NotNull(dummyResource); Assert.Equal("world", dummyResource.Name); }
public async Task SendAsync_InvalidJsonResponse_Error() { var httpClientMock = new HttpClientMock(); var httpClient = new DefaultHttpClient(httpClientMock.HttpClient); var client = new DummyClient(httpClient, null); var path = "/x"; var url = $"{MercadoPagoConfig.BaseUrl}{path}"; var request = new DummyRequest("hello"); var httpResponse = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("invalid json"), }; // Mock httpClientMock.MockRequest(url, System.Net.Http.HttpMethod.Post, httpResponse); try { await client.SendAsync(path, MercadoPagoCore.Http.HttpMethod.Post, request); } catch (MercadoPagoApiException ex) { Assert.Contains("Invalid response from API.", ex.Message); } }
public async Task SendAsync_WithRequestOptions_Success() { var httpClientMock = new HttpClientMock(); var httpClient = new DefaultHttpClient(httpClientMock.HttpClient); var client = new DummyClient(httpClient, null); var path = "/x"; var url = $"{MercadoPagoConfig.BaseUrl}{path}"; var request = new DummyRequest("hello"); var requestOptions = new RequestOptions { AccessToken = Guid.NewGuid().ToString(), RetryStrategy = new DefaultRetryStrategy(0), }; requestOptions.CustomHeaders.Add("X-Custom-Header", "custom"); requestOptions.CustomHeaders.Add(Headers.ContentType, "plain/text"); // should not add requestOptions.CustomHeaders.Add(Headers.Authorization, "Basic user:123"); // should not add var httpResponse = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{'name':'world'}"), }; httpResponse.Headers.Add("X-Custom-Response-Header", "456"); // Mock httpClientMock.MockRequest(url, System.Net.Http.HttpMethod.Put, httpResponse); DummyResource dummyResource = await client.SendAsync(path, MercadoPagoCore.Http.HttpMethod.Put, request, requestOptions); Assert.NotNull(dummyResource); Assert.Equal("world", dummyResource.Name); }
public async Task SendAsync_ErrorHttpStatusCode_Error() { var httpClientMock = new HttpClientMock(); var httpClient = new DefaultHttpClient(httpClientMock.HttpClient); var client = new DummyClient(httpClient, null); var path = "/x"; var url = $"{MercadoPagoConfig.BaseUrl}{path}"; var request = new DummyRequest(); var errorJson = @"{ ""message"": ""name attribute can't be null"", ""error"": ""bad_request"", ""status"": 400, ""cause"": [ { ""code"": 123, ""description"": ""name attribute can't be null"" } ] }"; var httpResponse = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(errorJson), }; // Mock httpClientMock.MockRequest(url, System.Net.Http.HttpMethod.Post, httpResponse); try { await client.SendAsync(path, MercadoPagoCore.Http.HttpMethod.Post, request); } catch (MercadoPagoApiException ex) { Assert.Equal(400, ex.StatusCode); Assert.NotNull(ex.ApiError); Assert.Equal(400, ex.ApiError.Status); Assert.Equal("bad_request", ex.ApiError.Error); Assert.Equal("name attribute can't be null", ex.ApiError.Message); Assert.NotNull(ex.ApiError.Cause); Assert.True(ex.ApiError.Cause.Count == 1); Assert.Equal("123", ex.ApiError.Cause[0].Code); } }