public void CannotGetOutputModelOnFailureCodeAsync() { var mockResponse = new MockResponse(404); var mockTransport = new MockTransport(mockResponse); PetStoreClient client = CreateClient(mockTransport); Assert.ThrowsAsync <RequestFailedException>(async() => await client.GetPetAsync("pet1")); }
public async Task ModelCastThrowsOnErrorCodeAsync() { var mockResponse = new MockResponse(404); // Send the response through the pipeline so IsError is set. var mockTransport = new MockTransport(mockResponse); PetStoreClient client = CreateClient(mockTransport); Response response = await client.GetPetAsync("pet1", ErrorOptions.NoThrow); Assert.Throws <RequestFailedException>(() => { Pet pet = response; }); }
public void ThrowOnErrorThrowsOnError() { var mockResponse = new MockResponse(404); var mockTransport = new MockTransport(mockResponse); PetStoreClient client = CreateClient(mockTransport); Assert.ThrowsAsync <RequestFailedException>(async() => { await client.GetPetAsync("snoopy", new RequestContext() { ErrorOptions = ErrorOptions.Default }); }); }
public async Task CanGetOutputModelOnSuccessCodeAsync() { var mockResponse = new MockResponse(200); Pet petResponse = new Pet("snoopy", "beagle"); mockResponse.SetContent(SerializationHelpers.Serialize(petResponse, SerializePet)); var mockTransport = new MockTransport(mockResponse); PetStoreClient client = CreateClient(mockTransport); Pet pet = await client.GetPetAsync("pet1"); Assert.AreEqual("snoopy", pet.Name); Assert.AreEqual("beagle", pet.Species); }
public async Task Change404Category() { var mockResponse = new MockResponse(404); // Send the response through the pipeline so IsError is set. var mockTransport = new MockTransport(mockResponse); PetStoreClient client = CreateClient(mockTransport); #region Snippet:SetRequestContext var context = new RequestContext(); context.AddClassifier(404, isError: false); Response response = await client.GetPetAsync("pet1", context); #endregion Assert.AreEqual(404, response.Status); }
public async Task CanGetResponseFromLlcGetMethodAsync() { var mockResponse = new MockResponse(200); Pet pet = new Pet("snoopy", "beagle"); mockResponse.SetContent(SerializationHelpers.Serialize(pet, SerializePet)); var mockTransport = new MockTransport(mockResponse); PetStoreClient client = CreateClient(mockTransport); Response response = await client.GetPetAsync("snoopy", new RequestOptions()); var doc = JsonDocument.Parse(response.Content.ToMemory()); Assert.AreEqual(200, response.Status); Assert.AreEqual("snoopy", doc.RootElement.GetProperty("name").GetString()); Assert.AreEqual("beagle", doc.RootElement.GetProperty("species").GetString()); }
public void CanSuppressExceptions() { var mockResponse = new MockResponse(404); var mockTransport = new MockTransport(mockResponse); PetStoreClient client = CreateClient(mockTransport); RequestContext context = new RequestContext() { ErrorOptions = ErrorOptions.NoThrow }; Response response = default; Assert.DoesNotThrowAsync(async() => { response = await client.GetPetAsync("snoopy", context); }); Assert.AreEqual(404, response.Status); }
public async Task GetRequestFailedException_StatusOptionDefault() { var mockResponse = new MockResponse(404); mockResponse.SetContent("{\"error\": { \"code\": \"MockStatusCode\" }}"); mockResponse.AddHeader(HttpHeader.Names.ContentType, "application/json"); var mockTransport = new MockTransport(mockResponse); PetStoreClient client = CreateClient(mockTransport); try { Pet pet = await client.GetPetAsync("pet1"); } catch (RequestFailedException e) { Assert.AreEqual(404, e.Status); Assert.That(() => e.Message.StartsWith("Service request failed.")); Assert.That(() => e.ErrorCode.StartsWith("MockStatusCode")); } }
public async Task ThrowOnErrorDoesntThrowOnSuccess() { var mockResponse = new MockResponse(200); Pet pet = new Pet("snoopy", "beagle"); mockResponse.SetContent(SerializationHelpers.Serialize(pet, SerializePet)); var mockTransport = new MockTransport(mockResponse); PetStoreClient client = CreateClient(mockTransport); Response response = await client.GetPetAsync("snoopy", new RequestContext() { ErrorOptions = ErrorOptions.Default }); var doc = JsonDocument.Parse(response.Content.ToMemory()); Assert.AreEqual(200, response.Status); Assert.AreEqual("snoopy", doc.RootElement.GetProperty("name").GetString()); Assert.AreEqual("beagle", doc.RootElement.GetProperty("species").GetString()); }
public async Task GetRequestFailedException_StatusOptionNoThrow() { var mockResponse = new MockResponse(404); mockResponse.SetContent("{\"error\": { \"code\": \"MockStatusCode\" }}"); mockResponse.AddHeader(HttpHeader.Names.ContentType, "application/json"); var mockTransport = new MockTransport(mockResponse); PetStoreClient client = CreateClient(mockTransport); try { // NOTE: is it weird that we're saying NoThrow here and it throws? // This looks confusing to me as someone reading this code. Pet pet = await client.GetPetAsync("pet1", ErrorOptions.NoThrow); } catch (RequestFailedException e) { Assert.AreEqual(404, e.Status); Assert.That(() => e.Message.StartsWith("Service request failed.")); Assert.That(() => e.ErrorCode.StartsWith("MockStatusCode")); } }