Skip to content

Commit 1354469

Browse files
committed
Add example for serialization to XML from record type
1 parent 4eecb50 commit 1354469

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

RestAssured.Net.Tests/XmlRequestBodySerializationTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,37 @@ public void ObjectCanBeSerializedToXml()
4646
.StatusCode(201);
4747
}
4848

49+
public record BlogPostRecord(int id, string title, string body)
50+
{
51+
/// <summary>
52+
/// Initializes a new instance of the <see cref="BlogPostRecord"/> class.
53+
/// </summary>
54+
public BlogPostRecord()
55+
: this(0, string.Empty, string.Empty)
56+
{
57+
}
58+
}
59+
60+
/// <summary>
61+
/// A test demonstrating RestAssuredNet syntax for serializing
62+
/// and sending an XML request body when performing an HTTP POST.
63+
/// </summary>
64+
[Test]
65+
public void RecordCanBeSerializedToXml()
66+
{
67+
this.CreateStubForXmlRequestBodyFromRecord();
68+
69+
var post = new BlogPostRecord(123, "My blog post title", "My blog post body");
70+
71+
Given()
72+
.ContentType("application/xml")
73+
.Body(post)
74+
.When()
75+
.Post($"{MOCK_SERVER_BASE_URL}/xml-serialization-from-record")
76+
.Then()
77+
.StatusCode(201);
78+
}
79+
4980
/// <summary>
5081
/// Verifies that the correct exception is thrown when the request body
5182
/// cannot be serialized based on the Content-Type header value.
@@ -79,5 +110,16 @@ private void CreateStubForXmlRequestBody()
79110
.RespondWith(Response.Create()
80111
.WithStatusCode(201));
81112
}
113+
114+
/// <summary>
115+
/// Creates the stub response for the XML request body example using a record.
116+
/// </summary>
117+
private void CreateStubForXmlRequestBodyFromRecord()
118+
{
119+
this.Server?.Given(Request.Create().WithPath("/xml-serialization-from-record").UsingPost()
120+
.WithBody(new XPathMatcher("//BlogPostRecord/id[text()='123']")))
121+
.RespondWith(Response.Create()
122+
.WithStatusCode(201));
123+
}
82124
}
83125
}

0 commit comments

Comments
 (0)