Skip to content

Commit beb9dda

Browse files
committed
Add support for supplying path to JSON file schema
1 parent b99a5ad commit beb9dda

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

RestAssured.Net.Tests/JsonSchemaValidationTests.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ public void JsonSchemaCanBeSuppliedAndVerifiedAsString()
4747
.MatchesJsonSchema(JsonSchemaDefinitions.MatchingJsonSchema);
4848
}
4949

50+
/// <summary>
51+
/// A test demonstrating RestAssuredNet syntax for validating a response
52+
/// against a JSON schema supplied as a string representing a file location.
53+
/// </summary>
54+
[Test]
55+
public void JsonSchemaCanBeSuppliedAndVerifiedAsStringPointingToFile()
56+
{
57+
this.CreateStubForJsonSchemaValidation();
58+
59+
Given()
60+
.When()
61+
.Get($"{MOCK_SERVER_BASE_URL}/json-schema-validation")
62+
.Then()
63+
.StatusCode(200)
64+
.And()
65+
.MatchesJsonSchema(@"../../../Schemas/matching.schema.json");
66+
}
67+
5068
/// <summary>
5169
/// A test demonstrating RestAssuredNet syntax for validating a response
5270
/// against a JSON schema supplied as a JsonSchema.
@@ -111,6 +129,50 @@ public void SupplyingInvalidJsonSchemaThrowsTheExpectedException()
111129
Assert.That(rve?.Message, Does.Contain("Could not parse supplied JSON schema. Error:"));
112130
}
113131

132+
/// <summary>
133+
/// A test checking that supplying an invalid JSON schema throws the expected exception.
134+
/// </summary>
135+
[Test]
136+
public void SupplyingInvalidJsonSchemaAsFileThrowsTheExpectedException()
137+
{
138+
this.CreateStubForJsonSchemaValidationMismatch();
139+
140+
var rve = Assert.Throws<ResponseVerificationException>(() =>
141+
{
142+
Given()
143+
.When()
144+
.Get($"{MOCK_SERVER_BASE_URL}/json-schema-validation-mismatch")
145+
.Then()
146+
.StatusCode(200)
147+
.And()
148+
.MatchesJsonSchema(@"../../../Schemas/invalid.schema.json");
149+
});
150+
151+
Assert.That(rve?.Message, Does.Contain("Could not parse supplied JSON schema. Error:"));
152+
}
153+
154+
/// <summary>
155+
/// A test checking that supplying an invalid JSON schema throws the expected exception.
156+
/// </summary>
157+
[Test]
158+
public void SupplyingNonsenseInputThrowsTheExpectedException()
159+
{
160+
this.CreateStubForJsonSchemaValidation();
161+
162+
var rve = Assert.Throws<ResponseVerificationException>(() =>
163+
{
164+
Given()
165+
.When()
166+
.Get($"{MOCK_SERVER_BASE_URL}/json-schema-validation")
167+
.Then()
168+
.StatusCode(200)
169+
.And()
170+
.MatchesJsonSchema("not-a-schema-not-a-file");
171+
});
172+
173+
Assert.That(rve?.Message, Does.Contain("Could not parse supplied JSON schema. Error:"));
174+
}
175+
114176
/// <summary>
115177
/// A test checking that a response with an unexpected Content-Type throws the expected exception.
116178
/// </summary>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"object",
3+
"properties": {
4+
"name": { "type": "string" },
5+
"hobbies": {
6+
"type": "array",
7+
"items": { "type": "string" }
8+
}
9+
}
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"name": {
5+
"type": "string"
6+
},
7+
"hobbies": {
8+
"type": "array",
9+
"items": {
10+
"type": "string"
11+
}
12+
}
13+
}
14+
}

RestAssured.Net/Response/VerifiableResponse.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,11 @@ public VerifiableResponse Body<T>(string path, IMatcher<IEnumerable<T>> matcher,
531531
/// <exception cref="ResponseVerificationException">Thrown when can't parse supplied JSON schema.</exception>
532532
public VerifiableResponse MatchesJsonSchema(string jsonSchema)
533533
{
534+
if (File.Exists(Path.GetFullPath(jsonSchema)))
535+
{
536+
jsonSchema = File.ReadAllText(Path.GetFullPath(jsonSchema));
537+
}
538+
534539
try
535540
{
536541
JsonSchema parsedSchema = JsonSchema.FromJsonAsync(jsonSchema).Result;

0 commit comments

Comments
 (0)