public void Returns200_IfTheServiceExists() { var testable = new TestableFemahApiHttpHandler(); SetupContextBaseMock("http://example.com/femah.axd/api/featureswitches"); _response.SetupProperty(x => x.StatusCode); var featureSwitches = new List<IFeatureSwitch> { new SimpleFeatureSwitch { Name = "TestFeatureSwitch", IsEnabled = false, FeatureType = "SimpleFeatureSwitch" } }; var providerMock = new Mock<IFeatureSwitchProvider>(); providerMock.Setup(p => p.AllFeatureSwitches()) .Returns(featureSwitches); Femah.Configure() .FeatureSwitchEnum(typeof(FemahApiTests.FeatureSwitches)) .Provider(providerMock.Object) .Initialise(); testable.ProcessRequest(_contextBase.Object); _response.Object.StatusCode.ShouldBe(200); }
public void ApiGetReturns200AndValidJsonArrayOfAllInitialisedFeatureSwitchesFromFeatureSwitchService() { //Arrange var testable = new TestableFemahApiHttpHandler(); var httpContextMock = new Mock<HttpContextBase>(); httpContextMock.Setup(x => x.Request.Url) .Returns(new Uri("http://example.com/femah.axd/api/featureswitches")); httpContextMock.SetupGet(x => x.Request.HttpMethod).Returns("GET"); var response = new Mock<HttpResponseBase>(); response.SetupProperty(x => x.StatusCode); httpContextMock.Setup(x => x.Response).Returns(response.Object); var featureSwitches = new List<IFeatureSwitch> { (new SimpleFeatureSwitch { Name = "TestFeatureSwitch1", IsEnabled = false, FeatureType = "SimpleFeatureSwitch" }), (new SimpleFeatureSwitch { Name = "TestFeatureSwitch2", IsEnabled = false, FeatureType = "SimpleFeatureSwitch" }) }; const string expectedJsonResponse = "[{\"IsEnabled\":false,\"Name\":\"TestFeatureSwitch1\",\"FeatureType\":\"SimpleFeatureSwitch\",\"Description\":\"Define a short description of the feature switch type here.\",\"ConfigurationInstructions\":\"Add configuration context and instructions to be displayed in the admin UI\"},{\"IsEnabled\":false,\"Name\":\"TestFeatureSwitch2\",\"FeatureType\":\"SimpleFeatureSwitch\",\"Description\":\"Define a short description of the feature switch type here.\",\"ConfigurationInstructions\":\"Add configuration context and instructions to be displayed in the admin UI\"}]"; var providerMock = new Mock<IFeatureSwitchProvider>(); providerMock.Setup(p => p.AllFeatureSwitches()) .Returns(featureSwitches); Femah.Configure() .FeatureSwitchEnum(typeof (FeatureSwitches)) .Provider(providerMock.Object) .Initialise(); //Get the JSON response by intercepting the call to context.Response.Write string responseContent = string.Empty; response.Setup(x => x.Write(It.IsAny<string>())).Callback((string r) => { responseContent = r; }); //Act testable.ProcessRequest(httpContextMock.Object); //Asert Assert.AreEqual(200, response.Object.StatusCode); Assert.AreEqual(expectedJsonResponse, responseContent); }
public void Returns405AndAccurateErrorMessage_IfServiceDoesNotSupportParameterQuerying() { var testable = new TestableFemahApiHttpHandler(); SetupContextBaseMock("http://example.com/femah.axd/api/featureswitchtypes/simplefeatureswitch"); _response.SetupProperty(x => x.StatusCode); var featureSwitchTypes = new[] { typeof(SimpleFeatureSwitch), typeof(SimpleFeatureSwitch) }; const string expectedJsonResponse = "\"Error: Service 'featureswitchtypes' does not support parameter querying.\""; Femah.Configure() .WithSelectedFeatureSwitchTypes(featureSwitchTypes) .Initialise(); //Get the JSON response by intercepting the call to context.Response.Write var responseContent = string.Empty; _response.Setup(x => x.Write(It.IsAny<string>())) .Callback((string r) => { responseContent = r; }); testable.ProcessRequest(_contextBase.Object); _response.Object.StatusCode.ShouldBe(405); responseContent.ShouldBe(expectedJsonResponse); }
public void ApiGetReturns200AndValidJsonArrayOfAllInitialisedFeatureSwitchTypesFromFeatureSwitchTypeService() { //Arrange var testable = new TestableFemahApiHttpHandler(); var httpContextMock = new Mock<HttpContextBase>(); httpContextMock.Setup(x => x.Request.Url) .Returns(new Uri("http://example.com/femah.axd/api/featureswitchtypes")); httpContextMock.SetupGet(x => x.Request.HttpMethod).Returns("GET"); var response = new Mock<HttpResponseBase>(); response.SetupProperty(x => x.StatusCode); httpContextMock.Setup(x => x.Response).Returns(response.Object); const int numberOfSwitchTypes = 2; var featureSwitchTypes = new Type[numberOfSwitchTypes]; featureSwitchTypes[0] = typeof (SimpleFeatureSwitch); featureSwitchTypes[1] = typeof(SimpleFeatureSwitch); const string expectedJsonResponse = "[{\"Name\":\"SimpleFeatureSwitch\",\"FeatureSwitchType\":\"Femah.Core.FeatureSwitchTypes.SimpleFeatureSwitch, Femah.Core, Version=0.1.0.0, Culture=neutral, PublicKeyToken=null\",\"Description\":\"Define a short description of the feature switch type here.\",\"ConfigurationInstructions\":\"Add configuration context and instructions to be displayed in the admin UI\"},{\"Name\":\"SimpleFeatureSwitch\",\"FeatureSwitchType\":\"Femah.Core.FeatureSwitchTypes.SimpleFeatureSwitch, Femah.Core, Version=0.1.0.0, Culture=neutral, PublicKeyToken=null\",\"Description\":\"Define a short description of the feature switch type here.\",\"ConfigurationInstructions\":\"Add configuration context and instructions to be displayed in the admin UI\"}]"; Femah.Configure() .WithSelectedFeatureSwitchTypes(featureSwitchTypes) .Initialise(); //Get the JSON response by intercepting the call to context.Response.Write string responseContent = string.Empty; response.Setup(x => x.Write(It.IsAny<string>())).Callback((string r) => { responseContent = r; }); //Act testable.ProcessRequest(httpContextMock.Object); //Assert Assert.AreEqual(200, response.Object.StatusCode); Assert.AreEqual(expectedJsonResponse, responseContent); }
public void ApiPutReturns415AndAccurateErrorMessageIfContentTypeIsNotSetToApplicationJson() { //Arrange var testable = new TestableFemahApiHttpHandler(); var httpContextMock = new Mock<HttpContextBase>(); httpContextMock.Setup(x => x.Request.Url) .Returns(new Uri("http://example.com/femah.axd/api/featureswitches/TestFeatureSwitch1")); httpContextMock.SetupGet(x => x.Request.HttpMethod).Returns("PUT"); httpContextMock.SetupGet(x => x.Request.ContentType).Returns("incorrect/contenttype"); var response = new Mock<HttpResponseBase>(); response.SetupProperty(x => x.StatusCode); httpContextMock.Setup(x => x.Response).Returns(response.Object); var featureSwitch = new SimpleFeatureSwitch { Name = "TestFeatureSwitch1", IsEnabled = false, FeatureType = "SimpleFeatureSwitch" }; const string expectedJsonResponse = "\"Error: Content-Type 'incorrect/contenttype' of request is not supported, expecting 'application/json'.\""; var providerMock = new Mock<IFeatureSwitchProvider>(); providerMock.Setup(p => p.Get(featureSwitch.Name.ToLower())) .Returns(featureSwitch); Femah.Configure() .FeatureSwitchEnum(typeof(FeatureSwitches)) .Provider(providerMock.Object) .Initialise(); //Get the JSON response by intercepting the call to context.Response.Write string responseContent = string.Empty; response.Setup(x => x.Write(It.IsAny<string>())).Callback((string r) => { responseContent = r; }); //Act testable.ProcessRequest(httpContextMock.Object); //Asert Assert.AreEqual(415, response.Object.StatusCode); Assert.AreEqual(expectedJsonResponse, responseContent); }
public void ApiGetReturns500AndAccurateErrorMessageIfRequestUrlIsInvalidAndContainsTooManySegments() { //apiRequest.ErrorMessage = string.Format("Error: The requested Url: '{0}' does not match the expected format /femah.axd/api/[service]/[parameter]", request.Url); //Arrange var testable = new TestableFemahApiHttpHandler(); var httpContextMock = new Mock<HttpContextBase>(); httpContextMock.Setup(x => x.Request.Url) .Returns(new Uri("http://example.com/femah.axd/api/invalid/url/structure")); httpContextMock.SetupGet(x => x.Request.HttpMethod).Returns("GET"); var response = new Mock<HttpResponseBase>(); response.SetupProperty(x => x.StatusCode); httpContextMock.Setup(x => x.Response).Returns(response.Object); var featureSwitch = new SimpleFeatureSwitch { Name = "TestFeatureSwitch1", IsEnabled = false, FeatureType = "SimpleFeatureSwitch" }; const string expectedJsonResponse = "\"Error: The requested Url 'http://example.com/femah.axd/api/invalid/url/structure' does not match the expected format /femah.axd/api/[service]/[parameter].\""; var providerMock = new Mock<IFeatureSwitchProvider>(); providerMock.Setup(p => p.Get(featureSwitch.Name.ToLower())) .Returns(featureSwitch); Femah.Configure() .FeatureSwitchEnum(typeof(FeatureSwitches)) .Provider(providerMock.Object) .Initialise(); //Get the JSON response by intercepting the call to context.Response.Write string responseContent = string.Empty; response.Setup(x => x.Write(It.IsAny<string>())).Callback((string r) => { responseContent = r; }); //Act testable.ProcessRequest(httpContextMock.Object); //Asert Assert.AreEqual(500, response.Object.StatusCode); Assert.AreEqual(expectedJsonResponse, responseContent); }
public void ApiGetReturns404AndEmptyResponseBodyIfFeatureSwitchNotfoundWithinFeatureSwitchService() { //Arrange var testable = new TestableFemahApiHttpHandler(); var httpContextMock = new Mock<HttpContextBase>(); httpContextMock.Setup(x => x.Request.Url) .Returns(new Uri("http://example.com/femah.axd/api/featureswitches/unknownfeatureswitch")); httpContextMock.SetupGet(x => x.Request.HttpMethod).Returns("GET"); var response = new Mock<HttpResponseBase>(); response.SetupProperty(x => x.StatusCode); httpContextMock.Setup(x => x.Response).Returns(response.Object); var featureSwitch = new SimpleFeatureSwitch { Name = "TestFeatureSwitch1", IsEnabled = false, FeatureType = "SimpleFeatureSwitch" }; const string expectedJsonResponse = "\"\""; var providerMock = new Mock<IFeatureSwitchProvider>(); providerMock.Setup(p => p.Get(featureSwitch.Name.ToLower())) .Returns(featureSwitch); Femah.Configure() .FeatureSwitchEnum(typeof(FeatureSwitches)) .Provider(providerMock.Object) .Initialise(); //Get the JSON response by intercepting the call to context.Response.Write string responseContent = string.Empty; response.Setup(x => x.Write(It.IsAny<string>())).Callback((string r) => { responseContent = r; }); //Act testable.ProcessRequest(httpContextMock.Object); //Asert Assert.AreEqual(404, response.Object.StatusCode); Assert.AreEqual(expectedJsonResponse, responseContent); }
public void Returns405_IfServiceIsNotFound() { var testable = new TestableFemahApiHttpHandler(); SetupContextBaseMock("http://example.com/femah.axd/api/unknownservicebla"); _response.SetupProperty(x => x.StatusCode); testable.ProcessRequest(_contextBase.Object); _response.Object.StatusCode.ShouldBe(405); }
public void ReturnsCorrectEncodingAndContentType() { var testable = new TestableFemahApiHttpHandler(); SetupContextBaseMock("http://example.com/femah.axd/api/featureswitchtypes"); _response.SetupProperty(x => x.ContentType); _response.SetupProperty(x => x.ContentEncoding); testable.ProcessRequest(_contextBase.Object); _response.Object.ContentType.ShouldBe("application/json"); _response.Object.ContentEncoding.ShouldBe(Encoding.UTF8); }