Skip to content

Commit

Permalink
Merge pull request #246 from dvonthenen/custom-headers-better-timeout
Browse files Browse the repository at this point in the history
Implements Custom Headers and Better Timeout for REST Interfaces
  • Loading branch information
dvonthenen authored Mar 25, 2024
2 parents 9a52ec9 + 18b57d5 commit 55641dc
Show file tree
Hide file tree
Showing 36 changed files with 1,393 additions and 827 deletions.
93 changes: 55 additions & 38 deletions Deepgram.Tests/UnitTests/ClientTests/AbstractRestClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,88 +27,97 @@ public void Setup()
[Test]
public void GetAsync_Should_Throws_HttpRequestException_On_UnsuccessfulResponse()
{
// Arrange
// Input and Output
var expectedResponse = new AutoFaker<ProjectsResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());

// Fake Clients
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y => y.GetAsync<ProjectsResponse>(UriSegments.PROJECTS))
client.Invoking(y => y.GetAsync<ProjectsResponse>($"{Defaults.DEFAULT_URI}/{UriSegments.PROJECTS}"))
.Should().ThrowAsync<HttpRequestException>();
}

[Test]
public void GetAsync_Should_Throws_Exception_On_UnsuccessfulResponse()
{
// Arrange
// Input and Output
var expectedResponse = new AutoFaker<ProjectsResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());

// Fake Clients
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y => y.GetAsync<ProjectsResponse>(UriSegments.PROJECTS))
client.Invoking(y => y.GetAsync<ProjectsResponse>($"{Defaults.DEFAULT_URI}/{UriSegments.PROJECTS}"))
.Should().ThrowAsync<Exception>();
}

// test that send stream content currently on in the prerecordedClient
[Test]
public void PostAsync_Which_Handles_HttpContent_Should_Throw_Exception_On_UnsuccessfulResponse()
{
// Arrange
// Input and Output
var response = new AutoFaker<SyncResponse>().Generate();
var httpContent = Substitute.For<HttpContent>();
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());

// Fake Clients
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y => y.PostAsync<SyncResponse>(UriSegments.PROJECTS, httpContent))
client.Invoking(y => y.PostAsync<HttpContent, SyncResponse>(UriSegments.PROJECTS, httpContent))
.Should().ThrowAsync<Exception>();
}

// test that send stream content currently on in the prerecordedClient
[Test]
public void PostAsync_Which_Handles_HttpContent_Should_Throw_HttpRequestException_On_UnsuccessfulResponse()
{
// Arrange
// Input and Output
var response = new AutoFaker<SyncResponse>().Generate();
var httpContent = Substitute.For<HttpContent>();

// Fake Clients
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y => y.PostAsync<SyncResponse>(UriSegments.PROJECTS, httpContent))
client.Invoking(y => y.PostAsync<HttpContent, SyncResponse>(UriSegments.PROJECTS, httpContent))
.Should().ThrowAsync<HttpRequestException>();
}


[Test]
public void PostAsync_Should_Throw_Exception_On_UnsuccessfulResponse()
{
// Arrange
// Input and Output
var response = new AutoFaker<SyncResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());

// Fake Clients
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y =>
y.PostAsync<SyncResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
y.PostAsync<StringContent, SyncResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
.Should().ThrowAsync<Exception>();
}

[Test]
public void PostAsync_Should_Throw_HttpRequestException_On_UnsuccessfulResponse()
{
// Arrange
// Input and Output
var response = new AutoFaker<SyncResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());

// Fake Clients
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y =>
y.PostAsync<SyncResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
y.PostAsync<StringContent, SyncResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
.Should().ThrowAsync<HttpRequestException>();
}

Expand All @@ -117,110 +126,118 @@ public void PostAsync_Should_Throw_HttpRequestException_On_UnsuccessfulResponse(
[Test]
public async Task Delete_Should_Throws_HttpRequestException_On_Response_Containing_Error()
{
// Arrange
// Input and Output
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());

// Fake Clients
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
await client.Invoking(async y => await y.DeleteAsync(UriSegments.PROJECTS))
await client.Invoking(async y => await y.DeleteAsync<MessageResponse>($"{Defaults.DEFAULT_URI}/{UriSegments.PROJECTS}"))
.Should().ThrowAsync<HttpRequestException>();
}

//Test for the delete calls that do not return a value
[Test]
public async Task Delete_Should_Throws_Exception_On_Response_Containing_Error()
{
// Arrange
// Input and Output
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());

// Fake Clients
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
await client.Invoking(async y => await y.DeleteAsync(UriSegments.PROJECTS))
await client.Invoking(async y => await y.DeleteAsync<MessageResponse>($"{Defaults.DEFAULT_URI}/{UriSegments.PROJECTS}"))
.Should().ThrowAsync<Exception>();
}


[Test]
public void DeleteAsync_TResponse_Should_Throws_HttpRequestException_On_UnsuccessfulResponse()
{
// Arrange
// Input and Output
var expectedResponse = new AutoFaker<MessageResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());

// Fake Clients
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y => y.DeleteAsync<MessageResponse>(UriSegments.PROJECTS))
client.Invoking(y => y.DeleteAsync<MessageResponse>($"{Defaults.DEFAULT_URI}/{UriSegments.PROJECTS}"))
.Should().ThrowAsync<HttpRequestException>();
}

[Test]
public void DeleteAsync_TResponse_Should_Throws_Exception_On_UnsuccessfulResponse()
{
// Arrange
// Input and Output
var expectedResponse = new AutoFaker<MessageResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());

// Fake Clients
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y => y.DeleteAsync<MessageResponse>(UriSegments.PROJECTS))
client.Invoking(y => y.DeleteAsync<MessageResponse>($"{Defaults.DEFAULT_URI}/{UriSegments.PROJECTS}"))
.Should().ThrowAsync<Exception>();
}

[Test]
public void PatchAsync_TResponse_Should_Throws_HttpRequestException_On_UnsuccessfulResponse()
{
// Arrange
// Input and Output
var expectedResponse = new AutoFaker<MessageResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());

// Fake Clients
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());
var client = new ConcreteRestClient(_apiKey, _clientOptions);

//Act & Assert
client.Invoking(y => y.PatchAsync<MessageResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
client.Invoking(y => y.PatchAsync<StringContent, MessageResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
.Should().ThrowAsync<HttpRequestException>();
}

[Test]
public void PatchAsync_TResponse_Should_Throws_Exception_On_UnsuccessfulResponse()
{
// Arrange
// Input and Output
var expectedResponse = new AutoFaker<MessageResponse>().Generate();
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());

// Fake Clients
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());
var client = new ConcreteRestClient(_apiKey, _clientOptions);

//Act & Assert
client.Invoking(y => y.PatchAsync<MessageResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
client.Invoking(y => y.PatchAsync<StringContent, MessageResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
.Should().ThrowAsync<Exception>();
}


[Test]
public void PutAsync_TResponse_Should_Throws_HttpRequestException_On_UnsuccessfulResponse()
{
// Arrange
/// Input and Output
var httpClient = MockHttpClient.CreateHttpClientWithException(new HttpRequestException());

// Fake Clients
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y => y.PutAsync<MessageResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
client.Invoking(y => y.PutAsync<StringContent, MessageResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
.Should().ThrowAsync<HttpRequestException>();
}

[Test]
public void PutAsync_TResponse_Should_Throws_Exception_On_UnsuccessfulResponse()
{
// Arrange
// Input and Output
var httpClient = MockHttpClient.CreateHttpClientWithException(new Exception());

// Fake Clients
var client = new ConcreteRestClient(_apiKey, _clientOptions);

// Act & Assert
client.Invoking(y => y.PutAsync<MessageResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
client.Invoking(y => y.PutAsync<StringContent, MessageResponse>(UriSegments.PROJECTS, new StringContent(string.Empty)))
.Should().ThrowAsync<Exception>();
}
}
Loading

0 comments on commit 55641dc

Please sign in to comment.