Skip to content

Commit

Permalink
Support objects (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttu authored Aug 30, 2018
1 parent 223627e commit 7f7af7f
Show file tree
Hide file tree
Showing 7 changed files with 352 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Add query support for sorting
* Fix for HEAD method use and ETag to support HEAD method
* .NET Core version to 2.1
* Support for objects

### [0.6.0] - 2017-11-30
* Caching of unchanged resources with ETag and If-None-Match headers
Expand Down
70 changes: 68 additions & 2 deletions FakeServer.Test/DynamicControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void GetCollections()
var controller = new DynamicController(ds, apiSettings);

var collections = controller.GetKeys();
Assert.Equal(3, collections.Count());
Assert.Equal(6, collections.Count());

UTHelpers.Down(filePath);
}
Expand Down Expand Up @@ -176,7 +176,7 @@ public void GetItems_UseResultObject()
{
var filePath = UTHelpers.Up();
var ds = new DataStore(filePath);
var apiSettings = Options.Create(new ApiSettings { UseResultObject = true });
var apiSettings = Options.Create(new ApiSettings { UseResultObject = true });

var controller = new DynamicController(ds, apiSettings);
controller.ControllerContext = new ControllerContext();
Expand Down Expand Up @@ -218,5 +218,71 @@ public void GetItems_UseResultObject_offsetlimit()

UTHelpers.Down(filePath);
}

[Fact]
public void GetItems_Single()
{
var filePath = UTHelpers.Up();
var ds = new DataStore(filePath);
var apiSettings = Options.Create(new ApiSettings { UpsertOnPut = true });

var controller = new DynamicController(ds, apiSettings);

var itemResult = controller.GetItems("configuration");
Assert.IsType<OkObjectResult>(itemResult);

var okObjectResult = itemResult as OkObjectResult;
dynamic item = okObjectResult.Value as ExpandoObject;
Assert.Equal("abba", item.password);

UTHelpers.Down(filePath);
}

[Fact]
public void GetItem_Single_BadRequest()
{
var filePath = UTHelpers.Up();
var ds = new DataStore(filePath);
var apiSettings = Options.Create(new ApiSettings { UpsertOnPut = true });

var controller = new DynamicController(ds, apiSettings);

var itemResult = controller.GetItem("configuration", "0");
Assert.IsType<BadRequestResult>(itemResult);

UTHelpers.Down(filePath);
}

[Fact]
public void GetNested_Single_BadRequest()
{
var filePath = UTHelpers.Up();
var ds = new DataStore(filePath);
var apiSettings = Options.Create(new ApiSettings());

var controller = new DynamicController(ds, apiSettings);

var result = controller.GetNested("configuration", 0, "ip");
Assert.IsType<BadRequestResult>(result);

UTHelpers.Down(filePath);
}

[Fact]
public async Task AddItem_Single_Conflict()
{
var filePath = UTHelpers.Up();
var ds = new DataStore(filePath);
var apiSettings = Options.Create(new ApiSettings());

var controller = new DynamicController(ds, apiSettings);

var item = new { value = "hello" };

var result = await controller.AddNewItem("configuration", JToken.FromObject(item));
Assert.IsType<ConflictResult>(result);

UTHelpers.Down(filePath);
}
}
}
97 changes: 97 additions & 0 deletions FakeServer.Test/FakeServerSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,103 @@ public async Task PostAndDeleteItem_NewCollection()
}
}

[Fact]
public async Task Post_SingleItem_ExistingCollection()
{
using (var client = new HttpClient())
{
var collection = "configuration";
var newConfig = new { value = "Something new" };

var content = new StringContent(JsonConvert.SerializeObject(newConfig), Encoding.UTF8, "application/json");
var result = await client.PostAsync($"{_fixture.BaseUrl}/api/{collection}", content);
Assert.Equal(HttpStatusCode.Conflict, result.StatusCode);
}
}

[Fact]
public async Task Put_SingleItem_ExistingCollection_BadRequest()
{
using (var client = new HttpClient())
{
var collection = "configuration";
var newConfig = new { value = "Something new" };

var content = new StringContent(JsonConvert.SerializeObject(newConfig), Encoding.UTF8, "application/json");
var result = await client.PutAsync($"{_fixture.BaseUrl}/api/{collection}/0", content);
Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode);
}
}

[Fact]
public async Task Put_SingleItem_ExistingItem()
{
using (var client = new HttpClient())
{
var collection = "configuration";
var newConfig = new { value = "Something new" };

var content = new StringContent(JsonConvert.SerializeObject(newConfig), Encoding.UTF8, "application/json");
var result = await client.PutAsync($"{_fixture.BaseUrl}/api/{collection}", content);
Assert.Equal(HttpStatusCode.NoContent, result.StatusCode);

result = await client.GetAsync($"{_fixture.BaseUrl}/api/{collection}");
result.EnsureSuccessStatusCode();
var item = JsonConvert.DeserializeObject<JObject>(await result.Content.ReadAsStringAsync());
Assert.Equal(newConfig.value, item["value"].Value<string>());
}
}

[Fact]
public async Task Put_CollectionItem_ExistingItem()
{
using (var client = new HttpClient())
{
var collection = "users";
var newUser = new { id = 1, name = "Newton" };

var content = new StringContent(JsonConvert.SerializeObject(newUser), Encoding.UTF8, "application/json");
var result = await client.PutAsync($"{_fixture.BaseUrl}/api/{collection}", content);
Assert.Equal(HttpStatusCode.BadRequest, result.StatusCode);
}
}

[Fact]
public async Task Patch_SingleItem()
{
using (var client = new HttpClient())
{
var collection = "configuration_for_patch";
var newConfig = new { url = "192.168.0.1" };

var content = new StringContent(JsonConvert.SerializeObject(newConfig), Encoding.UTF8, "application/json");
var result = await client.PatchAsync($"{_fixture.BaseUrl}/api/{collection}", content);
Assert.Equal(HttpStatusCode.NoContent, result.StatusCode);

result = await client.GetAsync($"{_fixture.BaseUrl}/api/{collection}");
result.EnsureSuccessStatusCode();
var item = JsonConvert.DeserializeObject<JObject>(await result.Content.ReadAsStringAsync());
Assert.Equal(newConfig.url, item["url"].Value<string>());
Assert.Equal("abba", item["password"].Value<string>());
}
}

[Fact]
public async Task Delete_SingleItem()
{
using (var client = new HttpClient())
{
var collection = "configuration_for_delete";
var newConfig = new { value = "Something new" };

var result = await client.DeleteAsync($"{_fixture.BaseUrl}/api/{collection}");
Assert.Equal(HttpStatusCode.NoContent, result.StatusCode);

result = await client.GetAsync($"{_fixture.BaseUrl}/api/{collection}");
Assert.Equal(HttpStatusCode.NotFound, result.StatusCode);
}
}

[Fact]
public async Task WebSockets_CalledTwice()
{
Expand Down
11 changes: 11 additions & 0 deletions FakeServer/Common/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using JsonFlatFileDataStore;

namespace FakeServer.Common
{
public static class ExtensionMethods
{
public static bool IsCollection(this IDataStore ds, string id) => ds.GetKeys(ValueType.Collection).ContainsKey(id);

public static bool IsItem(this IDataStore ds, string id) => ds.GetKeys(ValueType.Item).ContainsKey(id);
}
}
Loading

0 comments on commit 7f7af7f

Please sign in to comment.