|
8 | 8 | using System.Net.Http;
|
9 | 9 | using System.Text;
|
10 | 10 | using System.Threading.Tasks;
|
| 11 | +using System.Web.Http; |
11 | 12 | using Microsoft.Azure.WebJobs.Script.Binding;
|
| 13 | +using Microsoft.Azure.WebJobs.Script.WebHost; |
| 14 | +using Newtonsoft.Json; |
12 | 15 | using Newtonsoft.Json.Linq;
|
13 | 16 | using Xunit;
|
14 | 17 |
|
@@ -121,5 +124,93 @@ public async Task CreateResultContent_ExpandoObject_ReturnsJsonStringContent()
|
121 | 124 | Assert.Equal("Seattle", parsed["location"]);
|
122 | 125 | Assert.Equal("application/json", stringContent.Headers.ContentType.MediaType);
|
123 | 126 | }
|
| 127 | + |
| 128 | + [Fact] |
| 129 | + public async Task CreateResponse_JsonString_ReturnsExpectedResult() |
| 130 | + { |
| 131 | + HttpConfiguration config = new HttpConfiguration(); |
| 132 | + config.Formatters.Add(new PlaintextMediaTypeFormatter()); |
| 133 | + |
| 134 | + JObject child = new JObject |
| 135 | + { |
| 136 | + { "Name", "Mary" }, |
| 137 | + { "Location", "Seattle" }, |
| 138 | + { "Age", 5 } |
| 139 | + }; |
| 140 | + |
| 141 | + JObject parent = new JObject |
| 142 | + { |
| 143 | + { "Name", "Bob" }, |
| 144 | + { "Location", "Seattle" }, |
| 145 | + { "Age", 40 }, |
| 146 | + { "Children", new JArray(child) } |
| 147 | + }; |
| 148 | + string expectedBodyJson = parent.ToString(Formatting.None); |
| 149 | + |
| 150 | + // explicitly set a content type that there is no default |
| 151 | + // formatter for to force default non-negotiated content codepath |
| 152 | + JObject headers = new JObject |
| 153 | + { |
| 154 | + { "Content-Type", "foo/bar" } |
| 155 | + }; |
| 156 | + JObject responseObject = new JObject |
| 157 | + { |
| 158 | + { "Body", parent }, |
| 159 | + { "Headers", headers } |
| 160 | + }; |
| 161 | + HttpRequestMessage request = new HttpRequestMessage(); |
| 162 | + request.SetConfiguration(config); |
| 163 | + var response = HttpBinding.CreateResponse(request, responseObject.ToString()); |
| 164 | + string resultJson = await response.Content.ReadAsStringAsync(); |
| 165 | + Assert.Equal(expectedBodyJson, resultJson); |
| 166 | + Assert.Equal("foo/bar", response.Content.Headers.ContentType.MediaType); |
| 167 | + |
| 168 | + // Test again with a recognized content-type header, to force content negotiation |
| 169 | + headers = new JObject |
| 170 | + { |
| 171 | + { "Content-Type", "application/json" } |
| 172 | + }; |
| 173 | + responseObject = new JObject |
| 174 | + { |
| 175 | + { "Body", parent }, |
| 176 | + { "Headers", headers } |
| 177 | + }; |
| 178 | + request = new HttpRequestMessage(); |
| 179 | + request.SetConfiguration(config); |
| 180 | + response = HttpBinding.CreateResponse(request, responseObject.ToString()); |
| 181 | + resultJson = await response.Content.ReadAsStringAsync(); |
| 182 | + Assert.Equal(expectedBodyJson, resultJson); |
| 183 | + Assert.Equal("application/json", response.Content.Headers.ContentType.MediaType); |
| 184 | + |
| 185 | + // Test again with an explicitly specified response content type |
| 186 | + headers = new JObject |
| 187 | + { |
| 188 | + { "Content-Type", "text/plain" } |
| 189 | + }; |
| 190 | + responseObject = new JObject |
| 191 | + { |
| 192 | + { "Body", parent }, |
| 193 | + { "Headers", headers } |
| 194 | + }; |
| 195 | + request = new HttpRequestMessage(); |
| 196 | + request.SetConfiguration(config); |
| 197 | + response = HttpBinding.CreateResponse(request, responseObject.ToString()); |
| 198 | + resultJson = await response.Content.ReadAsStringAsync(); |
| 199 | + Assert.Equal(expectedBodyJson, resultJson); |
| 200 | + Assert.Equal("text/plain", response.Content.Headers.ContentType.MediaType); |
| 201 | + |
| 202 | + // Test again without an explicit response content type |
| 203 | + // to trigger ObjectContent negotiation codepath |
| 204 | + responseObject = new JObject |
| 205 | + { |
| 206 | + { "Body", parent } |
| 207 | + }; |
| 208 | + request = new HttpRequestMessage(); |
| 209 | + request.SetConfiguration(config); |
| 210 | + response = HttpBinding.CreateResponse(request, responseObject.ToString()); |
| 211 | + resultJson = await response.Content.ReadAsStringAsync(); |
| 212 | + Assert.Equal(expectedBodyJson, resultJson); |
| 213 | + Assert.Equal("application/json", response.Content.Headers.ContentType.MediaType); |
| 214 | + } |
124 | 215 | }
|
125 | 216 | }
|
0 commit comments