forked from restsharp/RestSharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStructuredSyntaxSuffixTests.cs
158 lines (119 loc) · 5.84 KB
/
StructuredSyntaxSuffixTests.cs
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
156
157
158
using System;
using System.Net;
using NUnit.Framework;
using RestSharp.Deserializers;
using RestSharp.IntegrationTests.Helpers;
namespace RestSharp.IntegrationTests
{
[TestFixture]
public class StructuredSyntaxSuffixTests
{
private class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
private const string XML_CONTENT = "<Person><name>Bob</name><age>50</age></Person>";
private const string JSON_CONTENT = @"{ ""name"":""Bob"", ""age"":50 }";
private static void QueryStringBasedContentAndContentTypeHandler(HttpListenerContext obj)
{
obj.Response.ContentType = obj.Request.QueryString["ct"];
obj.Response.OutputStream.WriteStringUtf8(obj.Request.QueryString["c"]);
obj.Response.StatusCode = 200;
}
[Test]
public void By_default_content_types_with_JSON_structured_syntax_suffix_should_deserialize_as_JSON()
{
Uri baseUrl = new Uri("http://localhost:8080/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, QueryStringBasedContentAndContentTypeHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest();
request.AddParameter("ct", "application/vnd.somebody.something+json");
request.AddParameter("c", JSON_CONTENT);
IRestResponse<Person> response = client.Execute<Person>(request);
Assert.AreEqual("Bob", response.Data.Name);
Assert.AreEqual(50, response.Data.Age);
}
}
[Test]
public void By_default_content_types_with_XML_structured_syntax_suffix_should_deserialize_as_XML()
{
Uri baseUrl = new Uri("http://localhost:8080/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, QueryStringBasedContentAndContentTypeHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest();
request.AddParameter("ct", "application/vnd.somebody.something+xml");
request.AddParameter("c", XML_CONTENT);
IRestResponse<Person> response = client.Execute<Person>(request);
Assert.AreEqual("Bob", response.Data.Name);
Assert.AreEqual(50, response.Data.Age);
}
}
[Test]
public void Content_type_that_matches_the_structured_syntax_suffix_format_but_was_given_an_explicit_handler_should_use_supplied_deserializer()
{
Uri baseUrl = new Uri("http://localhost:8080/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, QueryStringBasedContentAndContentTypeHandler))
{
RestClient client = new RestClient(baseUrl);
// In spite of the content type (+xml), treat this specific content type as JSON
client.AddHandler("application/vnd.somebody.something+xml", new JsonDeserializer());
RestRequest request = new RestRequest();
request.AddParameter("ct", "application/vnd.somebody.something+xml");
request.AddParameter("c", JSON_CONTENT);
IRestResponse<Person> response = client.Execute<Person>(request);
Assert.AreEqual("Bob", response.Data.Name);
Assert.AreEqual(50, response.Data.Age);
}
}
[Test]
public void Should_allow_wildcard_content_types_to_be_defined()
{
Uri baseUrl = new Uri("http://localhost:8080/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, QueryStringBasedContentAndContentTypeHandler))
{
RestClient client = new RestClient(baseUrl);
// In spite of the content type, handle ALL structured syntax suffixes of "+xml" as JSON
client.AddHandler("*+xml", new JsonDeserializer());
RestRequest request = new RestRequest();
request.AddParameter("ct", "application/vnd.somebody.something+xml");
request.AddParameter("c", JSON_CONTENT);
IRestResponse<Person> response = client.Execute<Person>(request);
Assert.AreEqual("Bob", response.Data.Name);
Assert.AreEqual(50, response.Data.Age);
}
}
[Test]
public void By_default_application_json_content_type_should_deserialize_as_JSON()
{
Uri baseUrl = new Uri("http://localhost:8080/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, QueryStringBasedContentAndContentTypeHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest();
request.AddParameter("ct", "application/json");
request.AddParameter("c", JSON_CONTENT);
IRestResponse<Person> response = client.Execute<Person>(request);
Assert.AreEqual("Bob", response.Data.Name);
Assert.AreEqual(50, response.Data.Age);
}
}
[Test]
public void By_default_text_xml_content_type_should_deserialize_as_XML()
{
Uri baseUrl = new Uri("http://localhost:8080/");
using (SimpleServer.Create(baseUrl.AbsoluteUri, QueryStringBasedContentAndContentTypeHandler))
{
RestClient client = new RestClient(baseUrl);
RestRequest request = new RestRequest();
request.AddParameter("ct", "text/xml");
request.AddParameter("c", XML_CONTENT);
IRestResponse<Person> response = client.Execute<Person>(request);
Assert.AreEqual("Bob", response.Data.Name);
Assert.AreEqual(50, response.Data.Age);
}
}
}
}