File: FileWebResponseTest.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (225 lines) | stat: -rw-r--r-- 6,552 bytes parent folder | download
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//
// FileWebResponseTest.cs - NUnit Test Cases for System.Net.FileWebResponse
//
// Authors:
//   Gert Driesen (drieseng@users.sourceforge.net)
//
// (C) 2007 Gert Driesen
//

using System;
using System.IO;
using System.Net;

using NUnit.Framework;



namespace MonoTests.System.Net
{
	[TestFixture]
	public class FileWebResponseTest
	{
		private string _tempDirectory;
		private string _tempFile;
		private Uri _tempFileUri;

		[SetUp]
		public void SetUp ()
		{
			_tempDirectory = Path.Combine (Path.GetTempPath (), "MonoTests.System.Net.FileWebResponseTest");
			_tempFile = Path.Combine (_tempDirectory, "FileWebResponseTest.tmp");
			if (!Directory.Exists (_tempDirectory)) {
				Directory.CreateDirectory (_tempDirectory);
			} else {
				// ensure no files are left over from previous runs
				string [] files = Directory.GetFiles (_tempDirectory, "*");
				foreach (string file in files)
					File.Delete (file);
			}
			_tempFileUri = GetTempFileUri ();
		}

		[TearDown]
		public void TearDown ()
		{
			if (Directory.Exists (_tempDirectory)) {
				string [] files = Directory.GetFiles (_tempDirectory, "*");
				foreach (string file in files)
					File.Delete (file);
				Directory.Delete (_tempDirectory, true);
			}
		}

		[Test]
		public void ContentLength ()
		{
			FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
			req.Method = "PUT";
			req.ContentLength = 100;
			using (Stream s = req.GetRequestStream ()) {
				s.WriteByte (72);
				s.WriteByte (110);
				s.WriteByte (80);
				s.Flush ();
			}
			req = (FileWebRequest) WebRequest.Create (_tempFileUri);
			using (FileWebResponse resp = (FileWebResponse) req.GetResponse ()) {
				Assert.AreEqual (3, resp.ContentLength, "#1");
				Assert.AreEqual (2, resp.Headers.Count, "#2");
				Assert.AreEqual ("Content-Length", resp.Headers.Keys [0], "#3");
				Assert.AreEqual ("3", resp.Headers.Get (0), "#4");
				resp.Headers.Clear ();
				Assert.AreEqual (3, resp.ContentLength, "#5");
				Assert.AreEqual (0, resp.Headers.Count, "#6");
			}
		}

		[Test]
		public void ContentType ()
		{
			FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
			req.Method = "PUT";
			req.ContentType = "image/png";
			using (Stream s = req.GetRequestStream ()) {
				s.WriteByte (72);
				s.WriteByte (110);
				s.WriteByte (80);
				s.Flush ();
			}
			req = (FileWebRequest) WebRequest.Create (_tempFileUri);
			using (FileWebResponse resp = (FileWebResponse) req.GetResponse ()) {
				Assert.AreEqual ("application/octet-stream", resp.ContentType, "#1");
				Assert.AreEqual (2, resp.Headers.Count, "#2");
				Assert.AreEqual ("Content-Type", resp.Headers.Keys [1], "#3");
				Assert.AreEqual ("application/octet-stream", resp.Headers.Get (1), "#4");
				resp.Headers.Clear ();
				Assert.AreEqual ("application/octet-stream", resp.ContentType, "#5");
				Assert.AreEqual (0, resp.Headers.Count, "#6");
			}
		}

		[Test]
		public void GetResponseStream ()
		{
			FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
			req.Method = "PUT";
			req.ContentType = "image/png";
			using (Stream s = req.GetRequestStream ()) {
				s.WriteByte (72);
				s.WriteByte (110);
				s.WriteByte (80);
				s.Flush ();
			}
			req = (FileWebRequest) WebRequest.Create (_tempFileUri);
			FileWebResponse respA = null;
			FileWebResponse respB = null;
			FileStream fsA = null;
			FileStream fsB = null;
			try {
				respA = (FileWebResponse) req.GetResponse ();
				fsA = respA.GetResponseStream () as FileStream;
				Assert.IsNotNull (fsA, "#A1");
				Assert.IsTrue (fsA.CanRead, "#A2");
				Assert.IsTrue (fsA.CanSeek, "#A3");
				Assert.IsFalse (fsA.CanTimeout, "#A4");
				Assert.IsFalse (fsA.CanWrite, "#A5");
				Assert.AreEqual (3, fsA.Length, "#A6");
				Assert.AreEqual (0, fsA.Position, "#A7");
				try {
					int i = fsA.ReadTimeout;
					Assert.Fail ("#A8:" + i);
				} catch (InvalidOperationException) {
				}
				try {
					int i = fsA.WriteTimeout;
					Assert.Fail ("#A9:" + i);
				} catch (InvalidOperationException) {
				}

				respB = (FileWebResponse) req.GetResponse ();
				fsB = respB.GetResponseStream () as FileStream;
				Assert.IsNotNull (fsB, "#B1");
				Assert.IsTrue (fsB.CanRead, "#B2");
				Assert.IsTrue (fsB.CanSeek, "#B3");
				Assert.IsFalse (fsB.CanTimeout, "#B4");
				Assert.IsFalse (fsB.CanWrite, "#B5");
				Assert.AreEqual (3, fsB.Length, "#B6");
				Assert.AreEqual (0, fsB.Position, "#B7");
				try {
					int i = fsB.ReadTimeout;
					Assert.Fail ("#B8:" + i);
				} catch (InvalidOperationException) {
				}
				try {
					int i = fsB.WriteTimeout;
					Assert.Fail ("#B9:" + i);
				} catch (InvalidOperationException) {
				}
			} finally {
				if (respA != null)
					respA.Close ();
				if (respB != null)
					respB.Close ();
			}
		}

		[Test]
		public void Headers ()
		{
			FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
			req.Method = "PUT";
			req.Headers.Add ("Disposition", "attach");
			using (Stream s = req.GetRequestStream ()) {
				s.WriteByte (72);
				s.WriteByte (110);
				s.WriteByte (80);
				s.Flush ();
			}
			req = (FileWebRequest) WebRequest.Create (_tempFileUri);
			using (FileWebResponse resp = (FileWebResponse) req.GetResponse ()) {
				Assert.AreEqual (2, resp.Headers.Count, "#1");
				Assert.AreEqual ("Content-Length", resp.Headers.Keys [0], "#2");
				Assert.AreEqual ("Content-Type", resp.Headers.Keys [1], "#3");
			}
		}

		[Test]
		public void ResponseUri ()
		{
			FileWebRequest req = (FileWebRequest) WebRequest.Create (_tempFileUri);
			req.Method = "PUT";
			req.ContentType = "image/png";
			using (Stream s = req.GetRequestStream ()) {
				s.WriteByte (72);
				s.WriteByte (110);
				s.WriteByte (80);
				s.Flush ();
			}
			req = (FileWebRequest) WebRequest.Create (_tempFileUri);
			using (FileWebResponse resp = (FileWebResponse) req.GetResponse ()) {
				Assert.AreEqual (_tempFileUri, resp.ResponseUri);
			}
		}

		private Uri GetTempFileUri ()
		{
			string tempFile = _tempFile;
			if (RunningOnUnix) {
				// remove leading slash for absolute paths
				tempFile = tempFile.TrimStart ('/');
			} else {
				tempFile = tempFile.Replace ('\\', '/');
			}
			return new Uri ("file:///" + tempFile);
		}
		private bool RunningOnUnix {
			get {
				// check for Unix platforms - see FAQ for more details
				// http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
				int platform = (int) Environment.OSVersion.Platform;
				return ((platform == 4) || (platform == 128));
			}
		}
	}
}