Skip to content

Commit 4eecb50

Browse files
committed
Update JSON serialization tests, add record example
1 parent 6debc60 commit 4eecb50

File tree

2 files changed

+25
-49
lines changed

2 files changed

+25
-49
lines changed

RestAssured.Net.Tests/JsonRequestBodySerializationTests.cs

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public void DictionaryCanBeSerializedToJson()
6262

6363
Dictionary<string, object> post = new Dictionary<string, object>
6464
{
65-
{ "Id", this.blogPost.Id },
66-
{ "Title", this.blogPost.Title },
67-
{ "Body", this.blogPost.Body },
65+
{ "id", this.blogPost.Id },
66+
{ "title", this.blogPost.Title },
67+
{ "body", this.blogPost.Body },
6868
};
6969

7070
Given()
@@ -86,9 +86,9 @@ public void AnonymousObjectCanBeSerializedToJson()
8686

8787
var post = new
8888
{
89-
Id = this.blogPost.Id,
90-
Title = this.blogPost.Title,
91-
Body = this.blogPost.Body,
89+
id = this.blogPost.Id,
90+
title = this.blogPost.Title,
91+
body = this.blogPost.Body,
9292
};
9393

9494
Given()
@@ -99,47 +99,27 @@ public void AnonymousObjectCanBeSerializedToJson()
9999
.StatusCode(201);
100100
}
101101

102+
private record BlogPostRecord(int id, string title, string body);
103+
102104
/// <summary>
103105
/// A test demonstrating RestAssuredNet syntax for serializing
104-
/// and sending an XML request body when performing an HTTP POST.
106+
/// a record type to JSON and sending it when performing an HTTP POST.
105107
/// </summary>
106108
[Test]
107-
public void ObjectCanBeSerializedToXml()
109+
public void RecordCanBeSerializedToJson()
108110
{
109-
this.CreateStubForXmlRequestBody();
111+
this.CreateStubForObjectSerialization();
112+
113+
var post = new BlogPostRecord(this.blogPost.Id, this.blogPost.Title, this.blogPost.Body);
110114

111115
Given()
112-
.ContentType("application/xml")
113-
.Body(this.GetLocation())
116+
.Body(post)
114117
.When()
115-
.Post($"{MOCK_SERVER_BASE_URL}/xml-serialization")
118+
.Post($"{MOCK_SERVER_BASE_URL}/object-serialization")
116119
.Then()
117120
.StatusCode(201);
118121
}
119122

120-
/// <summary>
121-
/// Verifies that the correct exception is thrown when the request body
122-
/// cannot be serialized based on the Content-Type header value.
123-
/// </summary>
124-
[Test]
125-
public void UnableToSerializeThrowsTheExpectedException()
126-
{
127-
this.CreateStubForXmlRequestBody();
128-
129-
var rce = Assert.Throws<RequestCreationException>(() =>
130-
{
131-
Given()
132-
.ContentType("application/something")
133-
.Body(this.GetLocation())
134-
.When()
135-
.Post($"{MOCK_SERVER_BASE_URL}/xml-serialization")
136-
.Then()
137-
.StatusCode(201);
138-
});
139-
140-
Assert.That(rce?.Message, Is.EqualTo("Could not determine how to serialize request based on specified content type 'application/something'"));
141-
}
142-
143123
/// <summary>
144124
/// Creates the stub response for the JSON string request body example.
145125
/// </summary>
@@ -162,17 +142,6 @@ private void CreateStubForObjectSerialization()
162142
.WithStatusCode(201));
163143
}
164144

165-
/// <summary>
166-
/// Creates the stub response for the XML request body example.
167-
/// </summary>
168-
private void CreateStubForXmlRequestBody()
169-
{
170-
this.Server?.Given(Request.Create().WithPath("/xml-serialization").UsingPost()
171-
.WithBody(new XPathMatcher("//Places[count(Place) = 2]")))
172-
.RespondWith(Response.Create()
173-
.WithStatusCode(201));
174-
}
175-
176145
private string GetExpectedSerializedObject()
177146
{
178147
return this.blogPost.GetSerializedJson();

RestAssured.Net.Tests/Models/BlogPost.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,25 @@ public class BlogPost
3535
/// </summary>
3636
public string Body { get; set; }
3737

38+
/// <summary>
39+
/// Initializes a new instance of the <see cref="BlogPost"/> class.
40+
/// </summary>
3841
public BlogPost()
3942
{
4043
this.Id = Faker.RandomNumber.Next();
4144
this.Title = Faker.Lorem.Sentence(5);
4245
this.Body = Faker.Lorem.Sentence(Faker.RandomNumber.Next(10, 20));
4346
}
4447

48+
/// <summary>
49+
/// Returns a JSON string representation of the <see cref="BlogPost"/> instance.
50+
/// </summary>
51+
/// <returns>A JSON string representation of the <see cref="BlogPost"/> instance.</returns>
4552
public string GetSerializedJson()
4653
{
47-
return "{\"Id\":" + this.Id +
48-
",\"Title\":\"" + this.Title +
49-
"\",\"Body\":\"" + this.Body + "\"}";
54+
return "{\"id\":" + this.Id +
55+
",\"title\":\"" + this.Title +
56+
"\",\"body\":\"" + this.Body + "\"}";
5057
}
5158
}
5259
}

0 commit comments

Comments
 (0)