forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOwinHostIntegrationTest.cs
More file actions
123 lines (106 loc) · 3.97 KB
/
Copy pathOwinHostIntegrationTest.cs
File metadata and controls
123 lines (106 loc) · 3.97 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin.Hosting;
using Microsoft.TestCommon;
using Newtonsoft.Json.Linq;
using Owin;
namespace System.Web.Http.Owin
{
[Xunit.Collection("PortReserver Collection")] // Avoid conflicts between different PortReserver consumers.
public class OwinHostIntegrationTest
{
[Fact]
public async Task SimpleGet_Works()
{
using (var port = new PortReserver())
using (WebApp.Start<OwinHostIntegrationTest>(url: CreateBaseUrl(port)))
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(CreateUrl(port, "HelloWorld"));
Assert.True(response.IsSuccessStatusCode);
Assert.Equal("\"Hello from OWIN\"", await response.Content.ReadAsStringAsync());
Assert.Null(response.Headers.TransferEncodingChunked);
}
}
[Fact]
public async Task SimplePost_Works()
{
using (var port = new PortReserver())
using (WebApp.Start<OwinHostIntegrationTest>(url: CreateBaseUrl(port)))
{
HttpClient client = new HttpClient();
var content = new StringContent("\"Echo this\"", Encoding.UTF8, "application/json");
var response = await client.PostAsync(CreateUrl(port, "Echo"), content);
Assert.True(response.IsSuccessStatusCode);
Assert.Equal("\"Echo this\"", await response.Content.ReadAsStringAsync());
Assert.Null(response.Headers.TransferEncodingChunked);
}
}
[Fact]
public async Task GetThatThrowsDuringSerializations_RespondsWith500()
{
using (var port = new PortReserver())
using (WebApp.Start<OwinHostIntegrationTest>(url: CreateBaseUrl(port)))
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(CreateUrl(port, "Error"));
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
JObject json = Assert.IsType<JObject>(JToken.Parse(await response.Content.ReadAsStringAsync()));
JToken exceptionMessage;
Assert.True(json.TryGetValue("ExceptionMessage", out exceptionMessage));
Assert.Null(response.Headers.TransferEncodingChunked);
}
}
private static string CreateBaseUrl(PortReserver port)
{
return port.BaseUri + "vroot";
}
private static string CreateUrl(PortReserver port, string localPath)
{
return CreateBaseUrl(port) + "/" + localPath;
}
#pragma warning disable xUnit1013 // Public method should be marked as test
public void Configuration(IAppBuilder appBuilder)
#pragma warning restore xUnit1013 // Public method should be marked as test
{
var config = new HttpConfiguration();
config.Routes.MapHttpRoute("Default", "{controller}");
appBuilder.UseWebApi(config);
}
}
public class HelloWorldController : ApiController
{
public string Get()
{
return "Hello from OWIN";
}
}
public class EchoController : ApiController
{
public string Post([FromBody] string s)
{
return s;
}
}
public class ErrorController : ApiController
{
public ExceptionThrower Get()
{
return new ExceptionThrower();
}
public class ExceptionThrower
{
public string Throws
{
get
{
throw new InvalidOperationException();
}
}
}
}
}