This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
19 additions
and
1,203 deletions.
There are no files selected for viewing
57 changes: 0 additions & 57 deletions
57
src/SlackConnector.Tests.Unit/Connections/Clients/RequestExecutorTests.cs
This file was deleted.
Oops, something went wrong.
286 changes: 18 additions & 268 deletions
286
src/SlackConnector.Tests.Unit/Connections/Clients/ResponseVerifierTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,285 +1,35 @@ | ||
using System.Net; | ||
using NUnit.Framework; | ||
using RestSharp; | ||
using NUnit.Framework; | ||
using Should; | ||
using SlackConnector.Connections.Clients; | ||
using SlackConnector.Connections.Models; | ||
using SlackConnector.Connections.Responses; | ||
using SlackConnector.Exceptions; | ||
using SpecsFor; | ||
using SpecsFor.ShouldExtensions; | ||
|
||
namespace SlackConnector.Tests.Unit.Connections.Clients | ||
{ | ||
public static class ResponseVerifierTests | ||
[TestFixture] | ||
public class ResponseVerifierTests | ||
{ | ||
internal class given_valid_response_then_should_return_expected_object : SpecsFor<ResponseVerifier> | ||
[Test] | ||
public void should_throw_exception_with_given_error_message_when_request_failed() | ||
{ | ||
private IRestResponse restResponse; | ||
private ExampleModel Result { get; set; } | ||
// given | ||
var response = new StandardResponse { Ok = false, Error = "I AM A ERROR-message" }; | ||
var verifier = new ResponseVerifier(); | ||
|
||
protected override void Given() | ||
{ | ||
restResponse = new RestResponse | ||
{ | ||
StatusCode = HttpStatusCode.OK, | ||
Content = @"{'ok': true, 'value': 'test'}" | ||
}; | ||
} | ||
|
||
protected override void When() | ||
{ | ||
Result = SUT.VerifyResponse<ExampleModel>(restResponse); | ||
} | ||
|
||
[Test] | ||
public void then_should_return_expected_model() | ||
{ | ||
var expected = new ExampleModel | ||
{ | ||
Ok = true, | ||
Value = "test" | ||
}; | ||
Result.ShouldLookLike(expected); | ||
} | ||
} | ||
|
||
internal class given_invalid_http_response_then_should_throw_exception : SpecsFor<ResponseVerifier> | ||
{ | ||
private IRestResponse restResponse; | ||
|
||
protected override void Given() | ||
{ | ||
restResponse = new RestResponse | ||
{ | ||
StatusCode = HttpStatusCode.BadRequest | ||
}; | ||
} | ||
|
||
|
||
[Test] | ||
public void then_should_throw_expected_exception() | ||
{ | ||
CommunicationException exception = null; | ||
|
||
try | ||
{ | ||
SUT.VerifyResponse<ExampleModel>(restResponse); | ||
} | ||
catch (CommunicationException ex) | ||
{ | ||
exception = ex; | ||
} | ||
|
||
string expectedMessage = $"Error occured while sending message '{restResponse.StatusCode}'"; | ||
exception.ShouldNotBeNull(); | ||
exception.Message.ShouldEqual(expectedMessage); | ||
} | ||
// when && then | ||
var exception = Assert.Throws<CommunicationException>(() => verifier.VerifyResponse(response)); | ||
exception.Message.ShouldEqual($"Error occured while posting message '{response.Error}'"); | ||
} | ||
|
||
internal class given_error_in_response_json_then_should_throw_exception : SpecsFor<ResponseVerifier> | ||
[Test] | ||
public void should_not_throw_exception() | ||
{ | ||
private IRestResponse restResponse; | ||
|
||
protected override void Given() | ||
{ | ||
restResponse = new RestResponse | ||
{ | ||
StatusCode = HttpStatusCode.OK, | ||
Content = @"{'ok': false, 'error': 'test error'}" | ||
}; | ||
} | ||
|
||
|
||
[Test] | ||
public void then_should_throw_expected_exception() | ||
{ | ||
CommunicationException exception = null; | ||
|
||
try | ||
{ | ||
SUT.VerifyResponse<ExampleModel>(restResponse); | ||
} | ||
catch (CommunicationException ex) | ||
{ | ||
exception = ex; | ||
} | ||
|
||
exception.ShouldNotBeNull(); | ||
exception.Message.ShouldEqual("Error occured while posting message 'test error'"); | ||
} | ||
} | ||
|
||
internal class given_join_channel_response : SpecsFor<ResponseVerifier> | ||
{ | ||
private IRestResponse _restResponse; | ||
private JoinChannelResponse Result { get; set; } | ||
|
||
protected override void Given() | ||
{ | ||
_restResponse = new RestResponse | ||
{ | ||
StatusCode = HttpStatusCode.OK, | ||
Content = @"{ | ||
'ok':true, | ||
'channel': { | ||
'id': 'my-id', | ||
'name': 'my-name', | ||
} | ||
}" | ||
}; | ||
} | ||
|
||
protected override void When() | ||
{ | ||
Result = SUT.VerifyResponse<JoinChannelResponse>(_restResponse); | ||
} | ||
|
||
[Test] | ||
public void should_return_expected_channel_response() | ||
{ | ||
var expected = new JoinChannelResponse | ||
{ | ||
Ok = true, | ||
Error = null, | ||
Channel = new Channel | ||
{ | ||
Id = "my-id", | ||
Name = "my-name" | ||
} | ||
}; | ||
|
||
Result.ShouldLookLike(expected); | ||
} | ||
} | ||
|
||
internal class given_handshake_response : SpecsFor<ResponseVerifier> | ||
{ | ||
private IRestResponse _restResponse; | ||
private HandshakeResponse Result { get; set; } | ||
|
||
protected override void Given() | ||
{ | ||
_restResponse = new RestResponse | ||
{ | ||
StatusCode = HttpStatusCode.OK, | ||
Content = Resources.ResourceManager.GetHandShakeResponseJson() | ||
}; | ||
} | ||
|
||
protected override void When() | ||
{ | ||
Result = SUT.VerifyResponse<HandshakeResponse>(_restResponse); | ||
} | ||
|
||
[Test] | ||
public void should_return_expected_channel_response() | ||
{ | ||
var expected = new HandshakeResponse | ||
{ | ||
Ok = true, | ||
Self = new Detail | ||
{ | ||
Id = "self-id", | ||
Name = "self-name" | ||
}, | ||
Team = new Detail | ||
{ | ||
Id = "team-id", | ||
Name = "team-name" | ||
}, | ||
Channels = new[] | ||
{ | ||
new Channel | ||
{ | ||
Id = "channel-id", | ||
Name = "channel-name", | ||
IsChannel = true, | ||
IsArchived = true, | ||
IsMember = true | ||
} | ||
}, | ||
Groups = new[] | ||
{ | ||
new Group | ||
{ | ||
Id = "group-id", | ||
Name = "group-name", | ||
IsGroup = true, | ||
IsArchived = true, | ||
IsOpen = true, | ||
Members = new [] | ||
{ | ||
"member1" | ||
} | ||
} | ||
}, | ||
Ims = new[] | ||
{ | ||
new Im | ||
{ | ||
Id = "im-id", | ||
User = "im-user", | ||
IsIm = true, | ||
IsOpen = true | ||
} | ||
}, | ||
Users = new[] | ||
{ | ||
new User | ||
{ | ||
Id = "user-id", | ||
Name = "user-name", | ||
Deleted = true, | ||
Profile = new Profile | ||
{ | ||
FirstName = "first-name", | ||
LastName = "last-name", | ||
RealName = "real-name", | ||
RealNameNormalised = "real-name-normalized", | ||
Email = "email" | ||
}, | ||
IsAdmin = true, | ||
IsBot = true | ||
} | ||
}, | ||
WebSocketUrl = @"wss://ms331.slack-msgs.com/websocket/999" | ||
}; | ||
|
||
Result.ShouldLookLike(expected); | ||
} | ||
} | ||
|
||
private class ExampleModel : StandardResponse | ||
{ | ||
public string Value { get; set; } | ||
} | ||
|
||
[TestFixture] | ||
public class ResponseVerifierTests2 | ||
{ | ||
[Test] | ||
public void should_throw_exception_with_given_error_message_when_request_failed() | ||
{ | ||
// given | ||
var response = new StandardResponse { Ok = false, Error = "I AM A ERROR-message" }; | ||
var verifier = new ResponseVerifier(); | ||
|
||
// when && then | ||
var exception = Assert.Throws<CommunicationException>(() => verifier.VerifyResponse(response)); | ||
exception.Message.ShouldEqual($"Error occured while posting message '{response.Error}'"); | ||
} | ||
|
||
[Test] | ||
public void should_not_throw_exception() | ||
{ | ||
// given | ||
var response = new StandardResponse { Ok = true }; | ||
var verifier = new ResponseVerifier(); | ||
// given | ||
var response = new StandardResponse { Ok = true }; | ||
var verifier = new ResponseVerifier(); | ||
|
||
// when && then | ||
Assert.DoesNotThrow(() => verifier.VerifyResponse(response)); | ||
} | ||
// when && then | ||
Assert.DoesNotThrow(() => verifier.VerifyResponse(response)); | ||
} | ||
} | ||
} |
Oops, something went wrong.