This repository was archived by the owner on Apr 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathModelBindingFixture.cs
More file actions
155 lines (128 loc) · 4.48 KB
/
Copy pathModelBindingFixture.cs
File metadata and controls
155 lines (128 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
namespace Nancy.Serialization.JsonNet.Tests
{
using System;
using System.Collections.Generic;
using Nancy.ModelBinding;
using Nancy.Testing;
using Xunit;
public class ModelBindingFixture
{
[Fact]
public void when_binding_to_a_class()
{
// Given
var module = new ConfigurableNancyModule(c => c.Post("/stuff", (_, m) =>
{
var stuff = m.Bind<Stuff>();
return stuff.Id.ToString();
}));
var bootstrapper = new TestBootstrapper(config => config.Module(module));
// When
var browser = new Browser(bootstrapper);
var result = browser.Post("/stuff", with =>
{
with.HttpRequest();
with.JsonBody(new Stuff(1), new JsonNetSerializer());
});
// Then
Assert.Equal(1, int.Parse(result.Body.AsString()));
}
[Fact]
public void when_binding_to_a_collection()
{
// Given
var module = new ConfigurableNancyModule(c => c.Post("/stuff", (_, m) =>
{
var stuff = m.Bind<List<Stuff>>();
return stuff.Count.ToString();
}));
var bootstrapper = new TestBootstrapper(config => config.Module(module));
// When
var browser = new Browser(bootstrapper);
var result = browser.Post("/stuff", with =>
{
with.HttpRequest();
with.JsonBody(new List<Stuff> {new Stuff(1), new Stuff(2)}, new JsonNetSerializer());
});
// Then
Assert.Equal(2, int.Parse(result.Body.AsString()));
}
[Fact]
public void Should_BindTo_Existing_Instance_Using_Body_Serializer()
{
//Given
var module = new ConfigurableNancyModule(c => c.Post("/instance", (_, m) =>
{
var model = new Stuff() { Id = 1 };
m.BindTo(model);
return model;
}));
var bootstrapper = new TestBootstrapper(config => config.Module(module));
var postmodel = new Stuff { Name = "Marsellus Wallace" };
var browser = new Browser(bootstrapper);
//When
var result = browser.Post("/instance", with =>
{
with.JsonBody(postmodel, new JsonNetSerializer());
with.Accept("application/json");
});
var resultModel = result.Body.DeserializeJson<Stuff>();
//Then
Assert.Equal("Marsellus Wallace", resultModel.Name);
Assert.Equal(1, resultModel.Id);
}
[Fact]
public void Should_BindTo_Existing_Instance_Using_Body_Serializer_And_BlackList()
{
//Given
var module = new ConfigurableNancyModule(c => c.Post("/instance", (_, m) =>
{
var model = new Stuff() { Id = 1 };
m.BindTo(model, new[]{"LastName"});
return model;
}));
var bootstrapper = new TestBootstrapper(config => config.Module(module));
var postmodel = new Stuff { Name = "Marsellus Wallace", LastName = "Smith"};
var browser = new Browser(bootstrapper);
//When
var result = browser.Post("/instance", with =>
{
with.JsonBody(postmodel, new JsonNetSerializer());
with.Accept("application/json");
});
var resultModel = result.Body.DeserializeJson<Stuff>();
//Then
Assert.Null(resultModel.LastName);
}
}
public class TestBootstrapper : ConfigurableBootstrapper
{
public TestBootstrapper(Action<ConfigurableBootstrapperConfigurator> configuration)
: base(configuration)
{
}
public TestBootstrapper()
{
}
protected override IEnumerable<Type> BodyDeserializers
{
get
{
yield return typeof(JsonNetBodyDeserializer);
}
}
}
public class Stuff
{
public Stuff()
{
}
public int Id { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
public Stuff(int id)
{
Id = id;
}
}
}