forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
135 lines (111 loc) · 3.8 KB
/
Copy pathProgram.cs
File metadata and controls
135 lines (111 loc) · 3.8 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
129
130
131
132
133
134
135
using System;
using System.Diagnostics;
using System.Net;
using Check.ServiceInterface;
using Funq;
using ServiceStack;
using ServiceStack.Host.Handlers;
using ServiceStack.Text;
namespace CheckHttpListener
{
public class AppHost : AppHostHttpListenerBase
{
public AppHost() : base("Check HttpListener Tests",
typeof(ErrorsService).Assembly,
typeof(UserService).Assembly) { }
public override void Configure(Container container)
{
//RawHttpHandlers.Add(_ => new CustomActionHandler((req, res) =>
//{
// var bytes = req.InputStream.ReadFully();
// res.OutputStream.Write(bytes, 0, bytes.Length);
//}));
this.CustomErrorHttpHandlers[HttpStatusCode.NotFound] = null;
SetConfig(new HostConfig
{
DebugMode = true,
SkipFormDataInCreatingRequest = true,
});
//Plugins.Add(new NativeTypesFeature());
}
}
class Program
{
static void Main(string[] args)
{
//Licensing.RegisterLicenseFromFileIfExists(@"c:\src\appsettings.license.txt");
new AppHost()
.Init()
.Start("http://*:2020/");
//TestService();
Process.Start("http://localhost:2020/upload.html");
//var response = "http://localhost:2020".PostToUrl(new { a = "foo", b = "bar" });
//"Response: {0}".Print(response);
Console.ReadLine();
}
private async static void TestService()
{
var client = new JsonServiceClient("http://localhost:2020/");
try
{
var request = new UpdateAddressVoid();
request.ToGetUrl().Print();
request.ToPostUrl().Print();
await client.PostAsync(request);
//var response = client.Post(request);
}
catch (WebServiceException ex)
{
ex.StatusCode.ToString().Print();
ex.StatusDescription.Print();
ex.ResponseBody.Print();
ex.ResponseStatus.PrintDump();
}
}
}
[Route("/user/{UserId}/Address")]
public class UpdateAddress : IReturn<AddressResponse>
{
public int UserId { get; set; }
public string Address { get; set; }
}
[Route("/user/{UserId}/AddressVoid")]
public class UpdateAddressVoid : IReturnVoid
{
public int UserId { get; set; }
public string Address { get; set; }
}
public class AddressResponse
{
public string Address { get; set; }
}
public class UserService : Service
{
public object Post(UpdateAddress request)
{
if (request.UserId > 0)
{
return new HttpResult(request.Address, HttpStatusCode.OK);
}
//throw new UnauthorizedAccessException("Unauthorized UserId");
//throw HttpError.Unauthorized(request.Address ?? "Unauthorized UserId");
return HttpError.Unauthorized(request.Address ?? "Unauthorized UserId");
}
public void Post(UpdateAddressVoid request)
{
//throw new UnauthorizedAccessException("Unauthorized UserId");
throw HttpError.Unauthorized(request.Address ?? "Unauthorized UserId");
//return HttpError.Unauthorized(request.Address ?? "Unauthorized UserId");
}
}
[Route("/upload", "POST")]
public class UploadFileRequest { }
public class TestController : Service
{
public void Any(UploadFileRequest request)
{
int filesCount = Request.Files.Length;
Console.WriteLine(filesCount); // Always 0
}
}
}