Here is the method that I’m testing:
I built this based on the following endpoint if it helps: https://dev.twitch.tv/docs/api/reference/#send-a-shoutout
public bool PostShoutOut(ShoutOut shoutOut)
{
string date = Date.GetCurrentDateTime();
string raiderName = shoutOut.RaiderName;
try
{
Dictionary<string, string> queryParams = new Dictionary<string, string>
{
{ "from_broadcaster_id", shoutOut.FromBroadcasterId },
{ "to_broadcaster_id", shoutOut.ToBroadcasterId },
{ "moderator_id", shoutOut.ModeratorId }
};
UriBuilder shoutOutUri = new UriBuilder(_baseUri)
{
Path = "chat/shoutouts",
Query = string.Join("&", queryParams.Select(kvp =>
$"{Uri.EscapeDataString(kvp.Key)}={Uri.EscapeDataString(kvp.Value)}"))
};
Uri finalUri = shoutOutUri.Uri;
HttpClient client = BuildClient();
HttpResponseMessage response = client.PostAsync(finalUri, null)
.GetAwaiter()
.GetResult();
string responseContent = response.Content.ReadAsStringAsync()
.GetAwaiter()
.GetResult();
if (!response.IsSuccessStatusCode)
{
HttpStatusCode statusCode = response.StatusCode;
string reason = response.ReasonPhrase;
_logger.LogError(
"[{date}] Failed to Shoutout: {raider}. Error Code: {code} -- Reason: {reason} -- Error Message: {message}",
date, raiderName, statusCode, reason, responseContent);
return false;
}
_logger.LogInformation("[{date}] Shouted Out: {raider} successfully!",
date, raiderName);
return true;
}
catch (Exception ex)
{
// _logger.LogError("Exception thrown when shouting out {raider}: {ex}",
// raiderName,
// ex.Message);
throw ex;
return false;
}
}
Here is my test:
[Test]
public void PostShoutOut_ShouldPostSuccessfully_ReturnTrue()
{
ShoutOut shoutOut = new ShoutOut("123456", "456231", "7895555", "TestName");
HttpResponseMessage expectedResponse = new HttpResponseMessage(HttpStatusCode.NoContent);
_mockHttpMessageHandler
.Protected()
.Setup<Task<HttpResponseMessage>>
(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
It.IsAny<CancellationToken>()
)
.ReturnsAsync(expectedResponse);
_httpClient = new HttpClient(_mockHttpMessageHandler.Object);
_mockHttpClientFactory.Setup(cf => cf.CreateClient(It.IsAny<string>())).Returns(_httpClient);
bool result = _httpClientRequests.PostShoutOut(shoutOut);
Assert.That(result, Is.True);
}
That issue that I’m facing is as soon as this line of code is ran it instantly falls into the exception block:
HttpResponseMessage response = client.PostAsync(finalUri, null)
.GetAwaiter()
.GetResult();
Anyone have any ideas?
If I just run the code in prod it works, it’s just the test that fails and idk why… I have another method that does a post and it works, so idk why this particular test is failing
The only difference I see between the working test and this failing one is that the working one is returning a 200, whereas this failing one is expected to return a 204