Skip to content

Commit

Permalink
Merge pull request #79 from lholman/69-api-returns-304-from-put
Browse files Browse the repository at this point in the history
#69 - API always returns 304 as response from PUT request
  • Loading branch information
hotstone committed Apr 6, 2014
2 parents 443520e + b579e6f commit 326843d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
1 change: 1 addition & 0 deletions Femah.Core.Tests/Femah.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="FemahApiRequestTests.cs" />
<Compile Include="FemahApiTests.cs" />
<Compile Include="FemahTests.cs" />
<Compile Include="InProcProviderTests.cs" />
<Compile Include="InvalidRequestTestData.cs" />
<Compile Include="PercentageFeatureSwitchTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
44 changes: 43 additions & 1 deletion Femah.Core.Tests/FemahApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public void ProcessPutRequestSetsHttpStatusCodeTo400AndReturnsGenericErrorMessag
}

[Test]
[Ignore("We are now testing this in the ApiResponseBuilder(), keep this as an integration test maybe?")]
//[Ignore("We are now testing this in the ApiResponseBuilder(), keep this as an integration test maybe?")]
public void ProcessPutRequestSetsHttpStatusCodeTo304AndReturnsAccurateErrorMessageInResponseBodyIfPutRequestBodyIsValidJsonButFeatureSwitchHasNoChanges()
{
//Arrange
Expand Down Expand Up @@ -361,6 +361,48 @@ public void ProcessPutRequestSetsHttpStatusCodeTo304AndReturnsAccurateErrorMessa
Assert.AreEqual(jsonRequestAndResponse, apiResponse.Body);
}


[Test]
//[Ignore("We are now testing this in the ApiResponseBuilder(), keep this as an integration test maybe?")]
public void ProcessPutRequestSetsHttpStatusCodeTo200AndReturnsUpdatedEntity()
{
//Arrange
const string validFeatureType = "Femah.Core.FeatureSwitchTypes.SimpleFeatureSwitch, Femah.Core, Version=0.1.0.0, Culture=neutral, PublicKeyToken=null";
string jsonRequestAndResponse = string.Format(
"{{\"IsEnabled\":true,\"Name\":\"TestFeatureSwitch1\",\"FeatureType\":\"{0}\",\"Description\":\"Define a short description of the feature switch type here.\",\"ConfigurationInstructions\":\"Add configuration context and instructions to be displayed in the admin UI\"}}",
validFeatureType);

var apiRequest = new ApiRequest
{
HttpMethod = "PUT",
Service = ApiRequest.ApiService.featureswitches,
Parameter = "TestFeatureSwitch",
Body = jsonRequestAndResponse
};

var featureSwitch = new SimpleFeatureSwitch
{
Name = "TestFeatureSwitch1",
IsEnabled = false,
FeatureType = validFeatureType
};

var providerMock = new Mock<IFeatureSwitchProvider>();
providerMock.Setup(p => p.Get("TestFeatureSwitch1"))
.Returns(featureSwitch);

Femah.Configure()
.FeatureSwitchEnum(typeof(FeatureSwitches))
.Provider(providerMock.Object)
.Initialise();

//Act
ApiResponse apiResponse = ProcessApiRequest.ProcessPutRequest(apiRequest);

//Assert
Assert.AreEqual((int)HttpStatusCode.OK, apiResponse.HttpStatusCode);
Assert.AreEqual(jsonRequestAndResponse, apiResponse.Body);
}
#endregion

#region General API (GET methods)
Expand Down
43 changes: 43 additions & 0 deletions Femah.Core.Tests/InProcProviderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Femah.Core.Providers;
using NUnit.Framework;

namespace Femah.Core.Tests
{
public class InProcProviderTests
{
public enum FeatureSwitches
{
SomeNewFeature = 1
}

public class TheGetMethod
{

[SetUp]
public void Initialize()
{
}

[Test]
public void FeatureTypeIsFullyQualifiedAssemblyName()
{
//Arrange
var inProcProvider = new InProcProvider();
const string expectedFullyQualifiedFeatureType = "Femah.Core.FeatureSwitchTypes.SimpleFeatureSwitch, Femah.Core, Version=0.1.0.0, Culture=neutral, PublicKeyToken=null";

//Act
Femah.Configure()
.FeatureSwitchEnum(typeof(FeatureSwitches))
.Initialise();

var featureSwitch = inProcProvider.Get("SomeNewFeature");

//Assert
Assert.AreEqual(expectedFullyQualifiedFeatureType, featureSwitch.FeatureType);


}
}

}
}
2 changes: 1 addition & 1 deletion Femah.Core/Api/ApiResponseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public ApiResponseBuilder WithHttpStatusCode(HttpStatusCode httpStatusCode)

//Updated and desired FeatureSwitch states DO match, return to client
if (updatedFeatureSwitchState.Equals(desiredfeatureSwitchState))
return SetResponseProperties(currentFeatureSwitchState.ToJson(), HttpStatusCode.OK);
return SetResponseProperties(updatedFeatureSwitchState.ToJson(), HttpStatusCode.OK);

//Updated and desired FeatureSwitch states do NOT match, roll back change
//This is probably not the best way to do this, TODO: maybe make all changes within a transaction?
Expand Down
2 changes: 1 addition & 1 deletion Femah.Core/Femah.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ internal static void SetSwitchType(string name, string typeName)
{
newFeatureSwitch.Name = featureSwitch.Name;
newFeatureSwitch.IsEnabled = featureSwitch.IsEnabled;
newFeatureSwitch.FeatureType = featureSwitch.GetType().Name;
newFeatureSwitch.FeatureType = featureSwitch.GetType().AssemblyQualifiedName;
}

// Save as the new type of feature switch.
Expand Down
2 changes: 1 addition & 1 deletion Femah.Core/Providers/InProcProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Initialise( IEnumerable<string> featureSwitches)

foreach (var featureSwitch in featureSwitches)
{
_featureSwitches.Add(new SimpleFeatureSwitch { Name = featureSwitch, IsEnabled = false, FeatureType = featureSwitch.GetType().Name});
_featureSwitches.Add(new SimpleFeatureSwitch { Name = featureSwitch, IsEnabled = false, FeatureType = typeof(SimpleFeatureSwitch).AssemblyQualifiedName });
}
}

Expand Down

0 comments on commit 326843d

Please sign in to comment.