public void GetResponse_FirstMatchesPath_ReturnsFirst() { // Arrange var random = new Mock <IRandomService>(); var response = new Response(); var action = new Mock <EndpointAction>(); action.Setup(m => m.GetResponse(It.IsAny <IRandomService>())) .Returns(response); var endpoint = new Mock <Endpoint>(); endpoint.Setup(m => m.MatchesPath(It.IsAny <string[]>(), out It.Ref <string[]> .IsAny)) .Callback(new MockMatchesPath((string[] path, out string[] remaining) => { remaining = new string[0]; })) .Returns(true); endpoint.Setup(m => m.GetActionWithMatchingMethod(It.IsAny <string>())) .Returns(action.Object); var endpoints = new List <Endpoint> { endpoint.Object }; var root = new EndpointsRoot { Endpoints = endpoints }; // Act var actualResponse = root.GetResponse("GET", "some/path", random.Object); // Assert Assert.Same(response, actualResponse); }
public EndpointsRoot GetEndpointsRoot() { lock (_responsesLock) { if (_responses is null) { _responses = LoadFromFile(); } } return(_responses); }
public void GetResponse_BothMatchPathButFirstDoesntMatchMethod_ReturnsSecond() { // Arrange var random = new Mock <IRandomService>(); var response1 = new Response(); var action1 = new Mock <EndpointAction>(); action1.Setup(m => m.GetResponse(It.IsAny <IRandomService>())) .Returns(response1); var endpoint1 = new Mock <Endpoint>(); endpoint1.Setup(m => m.MatchesPath(It.IsAny <string[]>(), out It.Ref <string[]> .IsAny)) .Callback(new MockMatchesPath((string[] path, out string[] remaining) => { remaining = new string[0]; })) .Returns(true); endpoint1.Setup(m => m.GetActionWithMatchingMethod(It.IsAny <string>())) .Returns <EndpointAction>(null); var response2 = new Response(); var action2 = new Mock <EndpointAction>(); action2.Setup(m => m.GetResponse(It.IsAny <IRandomService>())) .Returns(response2); var endpoint2 = new Mock <Endpoint>(); endpoint2.Setup(m => m.MatchesPath(It.IsAny <string[]>(), out It.Ref <string[]> .IsAny)) .Callback(new MockMatchesPath((string[] path, out string[] remaining) => { remaining = new string[0]; })) .Returns(true); endpoint2.Setup(m => m.GetActionWithMatchingMethod(It.IsAny <string>())) .Returns(action2.Object); var endpoints = new List <Endpoint> { endpoint1.Object, endpoint2.Object }; var root = new EndpointsRoot { Endpoints = endpoints }; // Act var actualResponse = root.GetResponse("GET", "some/path", random.Object); // Assert Assert.Same(response2, actualResponse); }
public void GetResponse_NullMethodAndPath_ReturnsNull(string method, string path) { // Arrange var random = new Mock <IRandomService>(); var root = new EndpointsRoot(); // Act var response = root.GetResponse(method, path, random.Object); // Assert Assert.Null(root.Endpoints); Assert.Equal(404, response.StatusCode); Assert.Null(response.StringBody); }
public void GetResponse_WithErrorMessage_ReturnsErrorResponse() { // Arrange var random = new Mock <IRandomService>(); var root = new EndpointsRoot("Error message"); // Act var response = root.GetResponse("ANY", "ANY", random.Object); // Assert Assert.Null(root.Endpoints); Assert.Equal(500, response.StatusCode); Assert.Equal("Error message", response.StringBody); }
public void GetResponse_NoParameters_CreatesEmptyEndpointRoot() { // Arrange var random = new Mock <IRandomService>(); var root = new EndpointsRoot(); // Act var response = root.GetResponse("ANY", "ANY", random.Object); // Assert Assert.Null(root.Endpoints); Assert.Equal(404, response.StatusCode); Assert.Null(response.StringBody); }
public void GetResponse_EmptyEndpoints_ReturnsNull() { // Arrange var random = new Mock <IRandomService>(); var root = new EndpointsRoot { Endpoints = new List <Endpoint>() }; // Act var response = root.GetResponse("GET", "/a/b/c", random.Object); // Assert Assert.Empty(root.Endpoints); Assert.Equal(404, response.StatusCode); Assert.Null(response.StringBody); }
public void Validate_NullEndpointsArray_ReturnsSingleError() { // Arrange var validator = new EndpointsRootValidator(_endpointValidator.Object); var section = new EndpointsRoot { Endpoints = null }; // Act var result = validator.Validate(section, _name); // Assert Assert.True(result.HasErrors); var error = Assert.Single(result.Errors); Assert.Equal(ErrorMessageFormatter.Format("$.endpoints", "Endpoints array is null"), error); }
public void Validate_EmptyEndpointsArray_ReturnsSuccessWithoutCallingEndpointValidator() { // Arrange var validator = new EndpointsRootValidator(_endpointValidator.Object); var section = new EndpointsRoot { Endpoints = new List <Endpoint>() }; // Act var result = validator.Validate(section, _name); // Assert Assert.False(result.HasErrors); _endpointValidator.Verify(m => m.Validate( It.IsAny <Endpoint>(), It.IsAny <SectionName>() ), Times.Never()); }
public void Validate_MultipleEndpointsInArray_CallsEndpointValidatorForAllEndpoints() { // Arrange var validator = new EndpointsRootValidator(_endpointValidator.Object); var endpoint1 = new Endpoint(); var endpoint2 = new Endpoint(); var endpoint3 = new Endpoint(); var section = new EndpointsRoot { Endpoints = new List <Endpoint> { endpoint1, endpoint2, endpoint3 } }; // Act var result = validator.Validate(section, _name); // Assert Assert.False(result.HasErrors); _endpointValidator.Verify(m => m.Validate( endpoint1, It.Is <SectionName>(s => s.PropertyPath == "$.endpoints[0]") ), Times.Once()); _endpointValidator.Verify(m => m.Validate( endpoint2, It.Is <SectionName>(s => s.PropertyPath == "$.endpoints[1]") ), Times.Once()); _endpointValidator.Verify(m => m.Validate( endpoint3, It.Is <SectionName>(s => s.PropertyPath == "$.endpoints[2]") ), Times.Once()); }