forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpResultContentTypeTests.cs
More file actions
128 lines (108 loc) · 4.21 KB
/
Copy pathHttpResultContentTypeTests.cs
File metadata and controls
128 lines (108 loc) · 4.21 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
124
125
126
127
128
using System;
using System.IO;
using System.Net;
using NUnit.Framework;
using ServiceStack.Logging;
using ServiceStack.Web;
namespace ServiceStack.WebHost.Endpoints.Tests
{
[TestFixture]
public class HttpResultContentTypeTests {
#region setup for example plaintext service
public class SimpleAppHostHttpListener : AppHostHttpListenerBase {
//Tell Service Stack the name of your application and where to find your web services
public SimpleAppHostHttpListener()
: base("Test Services", typeof(SimpleAppHostHttpListener).Assembly) {
LogManager.LogFactory = new TestLogFactory();
}
/// <summary>
/// AppHostHttpListenerBase method.
/// </summary>
/// <param name="container">SS's funq container</param>
public override void Configure(Funq.Container container) {
HostContext.Config.GlobalResponseHeaders.Clear();
//Signal advanced web browsers what HTTP Methods you accept
//base.SetConfig(new EndpointHostConfig());
Routes.Add<PlainText>("/test/plaintext", "GET");
}
}
/// <summary>
/// *Request* DTO
/// </summary>
public class PlainText {
/// <summary>
/// Controls if the service calls response.ContentType or just new HttpResult
/// </summary>
public bool SetContentTypeBrutally { get; set; }
/// <summary>
/// Text to respond with
/// </summary>
public string Text { get; set; }
}
public class TimedService : Service
{
public object Any(PlainText request)
{
string contentType = "text/plain";
var response = new HttpResult(request.Text, contentType);
if(request.SetContentTypeBrutally) {
response.ContentType = contentType;
}
return response;
}
}
#endregion
private const string ListeningOn = "http://localhost:1337/";
SimpleAppHostHttpListener appHost;
public HttpResultContentTypeTests()
{
}
[TestFixtureSetUp]
public void OnTestFixtureStartUp()
{
appHost = new SimpleAppHostHttpListener();
appHost.Init();
appHost.Start(ListeningOn);
System.Console.WriteLine("ExampleAppHost Created at {0}, listening on {1}",
DateTime.Now, ListeningOn);
}
[TestFixtureTearDown]
public void OnTestFixtureTearDown()
{
appHost.Dispose();
//Clear the logs so other tests dont inherit log entries
TestLogger.GetLogs().Clear();
}
/// <summary>
/// This test calls a simple web service which uses HttpResult(string responseText, string contentType) constructor
/// to set the content type.
///
///
/// </summary>
/// <param name="setContentTypeBrutally">If true the service additionally 'brutally' sets the content type after using HttpResult constructor which should do it anyway</param>
//This test case fails on mono 2.6.7 (the content type is 'text/html')
[TestCase(false)]
//This test case passes on mono 2.6.7
[TestCase(true)]
public void TestHttpRestulSettingContentType(bool setContentTypeBrutally) {
string text = "Some text";
string url = string.Format("{0}/test/plaintext?SetContentTypeBrutally={1}&Text={2}", ListeningOn, setContentTypeBrutally,text);
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
HttpWebResponse res = null;
try {
res = (HttpWebResponse)req.GetResponse();
string downloaded;
using(StreamReader s = new StreamReader(res.GetResponseStream())) {
downloaded = s.ReadToEnd();
}
Assert.AreEqual(text, downloaded, "Checking the downloaded string");
Assert.AreEqual("text/plain", res.ContentType, "Checking for expected contentType" );
}
finally {
if(res != null) {
res.Close();
}
}
}
}
}