Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FileWebRequestTest.cs « System.Net « Test « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ae99aa076a0e950d9d3dad3c5d05a9362ceebd5c (plain)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// FileWebRequestTest.cs - NUnit Test Cases for System.Net.FileWebRequest
//
// Author:
//   Lawrence Pit (loz@cable.a2000.nl)
//

using NUnit.Framework;
using System;
using System.IO;
using System.Net;
using System.Collections;
using System.Security;
using System.Security.Permissions;

namespace MonoTests.System.Net
{

public class FileWebRequestTest : TestCase
{
        public FileWebRequestTest () :
                base ("[MonoTests.System.Net.FileWebRequestTest]") {}

        public FileWebRequestTest (string name) : base (name) {}

        protected override void SetUp () {}

        protected override void TearDown () {}

        public static ITest Suite
        {
                get {
                        return new TestSuite (typeof (FileWebRequestTest));
                }
        }
        
        
        public void TestAsync ()
        {
		string tmpFilename = GetFilename ();
		if (tmpFilename == null) {
			Console.WriteLine ("\n\nSet environment variable TMPDIR to a temporary directory to test FileWebRequest\n");
			return;
		}
		
		try {
			if (File.Exists (tmpFilename)) 
				File.Delete (tmpFilename);
			
			Uri uri = new Uri ("file:///" + tmpFilename);
			
			WebRequest req = WebRequest.Create (uri);
			req.Method = "PUT";
			
			req.Timeout = 2 * 1000;
			IAsyncResult async = req.BeginGetRequestStream (null, null);
			try {
				req.BeginGetRequestStream (null, null);
				Fail ("#1 should've failed");
			} catch (InvalidOperationException) { 
				//Console.WriteLine ("GOT1: " + e.Message + "\n" + e.StackTrace);				
				// Cannot re-call BeginGetRequestStream/BeginGetResponse while
				// a previous call is still in progress
			}
			/*
			try {
				req.BeginGetResponse (null, null);
				Fail ("#2 should've failed");
			} catch (InvalidOperationException) { }
			*/
			try {
				req.GetRequestStream ();
				Fail ("#3 should've failed");
			} catch (InvalidOperationException) { 
				// Console.WriteLine ("GOT3: " + e.Message + "\n" + e.StackTrace);
				// Cannot re-call BeginGetRequestStream/BeginGetResponse while
				// a previous call is still in progress
			}

			try {
				req.GetResponse ();
				Fail ("#4 should've failed");
			} catch (WebException) { 
				// Console.WriteLine ("4: " + e.Message + "\n" + e.StackTrace);				
				// The operation has timed out
			}

			try {
				IAsyncResult async0 = req.BeginGetResponse (null, null);
				req.EndGetResponse (async0);
				// Console.WriteLine ("X5c");
				Fail ("#5 should've failed");
			} catch (InvalidOperationException) { 
				// Console.WriteLine ("5e: " + e.Message + "\n" + e.StackTrace);
				// Cannot re-call BeginGetRequestStream/BeginGetResponse while
				// a previous call is still in progress
			}
			
			// Console.WriteLine ("WEBHEADERS: " + req.Headers);
			
			Stream wstream = req.EndGetRequestStream (async);
			AssertEquals ("#1r", false, wstream.CanRead);
			AssertEquals ("#1w", true, wstream.CanWrite);
			AssertEquals ("#1s", true, wstream.CanSeek);

			wstream.WriteByte (72);
			wstream.WriteByte (101);
			wstream.WriteByte (108);
			wstream.WriteByte (108);
			wstream.WriteByte (111);
			wstream.Close ();
			
			// stream written

			req = WebRequest.Create (uri);
			WebResponse res = req.GetResponse ();	
			
			try {
				req.BeginGetRequestStream (null, null);
				Fail ("#20: should've failed");
			} catch (InvalidOperationException) { 
				// Console.WriteLine ("20: " + e.Message + "\n" + e.StackTrace);				
				// Cannot send a content-body with this verb-type
			}
			
			try {
				req.Method = "PUT";
				req.BeginGetRequestStream (null, null);
				Fail ("#21: should've failed");
			} catch (InvalidOperationException) { 
				// Console.WriteLine ("21: " + e.Message + "\n" + e.StackTrace);				
				// This operation cannot be perfomed after the request has been submitted.
			}
			
			try {
				//IAsyncResult async2 = req.BeginGetResponse (null, null);
				//Console.WriteLine ("OK!");
				req.GetResponse ();
				//Fail ("#22: should've failed");
			} catch (InvalidOperationException) { 
				//Console.WriteLine ("22: " + e.Message + "\n" + e.StackTrace);
				// Cannot re-call BeginGetRequestStream/BeginGetResponse while
				// a previous call is still in progress
				Fail ("#22: should not have failed");
			}			
			
			try {
				IAsyncResult async2 = req.BeginGetResponse (null, null);
				
				// this succeeds !!
				
				try {
					WebResponse res2 = req.EndGetResponse (async2);
										
					// and this succeeds
					
					AssertEquals ("#23", res, res2) ;
					
					//Fail ("#23: should've failed");
				} catch (InvalidOperationException) { 
					//Console.WriteLine ("22: " + e.Message + "\n" + e.StackTrace);				
					// Cannot re-call BeginGetRequestStream/BeginGetResponse while
					// a previous call is still in progress
				}				
				
				// Fail ("#22: should've failed");
			} catch (InvalidOperationException) { 
			}			

			AssertEquals ("#2 len", (long) 5, res.ContentLength);
			AssertEquals ("#2 type", "binary/octet-stream", res.ContentType);
			AssertEquals ("#2 scheme", "file", res.ResponseUri.Scheme);
			
			Stream rstream = res.GetResponseStream ();			
			AssertEquals ("#3r", true, rstream.CanRead);
			AssertEquals ("#3w", false, rstream.CanWrite);
			AssertEquals ("#3s", true, rstream.CanSeek);
			
			AssertEquals ("#4a", 72, rstream.ReadByte ());
			AssertEquals ("#4b", 101, rstream.ReadByte ());
			AssertEquals ("#4c", 108, rstream.ReadByte ());

			rstream.Close ();
			// res.Close ();
			
			try {
				long len = res.ContentLength;
				AssertEquals ("#5", (long) 5, len);
			} catch (ObjectDisposedException) {
				Fail ("#disposed contentlength");				
			}
			try {
				WebHeaderCollection w = res.Headers;
			} catch (ObjectDisposedException) {
				Fail ("#disposed headers");				
			}			
			try {
				res.Close ();				
			} catch (ObjectDisposedException) {
				Fail ("#disposed close");				
			}
		} catch (Exception) {
			// Console.WriteLine ("ERROR! : " + ee.Message + "\n" + ee.StackTrace);
		} finally {
			try {
				// known bug #24940
				File.Delete (tmpFilename);
			} catch (Exception) { 
				// Console.WriteLine ("ERROR2! : " + ee2.Message + "\n" + ee2.StackTrace);
			}
		}
	}	        
        
        public void TestSync ()
        {
		string tmpFilename = GetFilename ();
		if (tmpFilename == null)
			return;
		
		try {		
			if (File.Exists (tmpFilename)) 
				File.Delete (tmpFilename);
			
			Uri uri = new Uri ("file:///" + tmpFilename);
			
			WebRequest req = WebRequest.Create (uri);
			
			try {
				Stream stream = req.GetRequestStream ();
				Fail ("should throw exception");
			} catch (ProtocolViolationException) {}
			
			req.Method = "PUT";
			
			Stream wstream = req.GetRequestStream ();
			AssertEquals ("#1r", false, wstream.CanRead);
			AssertEquals ("#1w", true, wstream.CanWrite);
			AssertEquals ("#1s", true, wstream.CanSeek);

			wstream.WriteByte (72);
			wstream.WriteByte (101);
			wstream.WriteByte (108);
			wstream.WriteByte (108);
			wstream.WriteByte (111);
			wstream.Close ();
			
			// stream written
			
			req = WebRequest.Create (uri);
			WebResponse res = req.GetResponse ();			
			AssertEquals ("#2 len", (long) 5, res.ContentLength);
			AssertEquals ("#2 type", "binary/octet-stream", res.ContentType);
			AssertEquals ("#2 scheme", "file", res.ResponseUri.Scheme);
			
			Stream rstream = res.GetResponseStream ();			
			AssertEquals ("#3r", true, rstream.CanRead);
			AssertEquals ("#3w", false, rstream.CanWrite);
			AssertEquals ("#3s", true, rstream.CanSeek);
			
			AssertEquals ("#4a", 72, rstream.ReadByte ());
			AssertEquals ("#4b", 101, rstream.ReadByte ());
			AssertEquals ("#4c", 108, rstream.ReadByte ());
			
			rstream.Close ();
			// res.Close ();
			
			try {
				long len = res.ContentLength;
				AssertEquals ("#5", (long) 5, len);
			} catch (ObjectDisposedException) {
				Fail ("#disposed contentlength");				
			}
			try {
				WebHeaderCollection w = res.Headers;
			} catch (ObjectDisposedException) {
				Fail ("#disposed headers");				
			}			
			try {
				res.Close ();				
			} catch (ObjectDisposedException) {
				Fail ("#disposed close");				
			}
			
		} finally {
			try {
				File.Delete (tmpFilename);
			} catch (Exception) { }
		}
	}	
	
	private string GetFilename ()
	{
		string tmpdir = Environment.GetEnvironmentVariable ("TMPDIR");
		if (tmpdir == null || tmpdir.Length == 0) {
			return null;
		}
		
		tmpdir = tmpdir.Replace ('\\', '/');
		if (tmpdir [tmpdir.Length - 1] != '/')
			tmpdir += "/";
		return tmpdir + "FileWebRequestTest.tmp";
	}
}

}