-
Notifications
You must be signed in to change notification settings - Fork 4
/
UploadFile.cs
115 lines (90 loc) · 4.18 KB
/
UploadFile.cs
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
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Refit;
using RefitVsRestSharp.Test.Apis;
using RestSharp;
using Xunit;
namespace RefitVsRestSharp.Test
{
public sealed class UploadFile
{
private const string FileContent = "File Content";
[Fact]
public async Task DoWithRestSharp()
{
var client = new RestClient("http://localhost:5000");
var request = new RestRequest("files/upload", Method.POST);
byte[] bytes = Encoding.UTF8.GetBytes(FileContent);
request.AddFile("file", bytes, "filename.txt");
IRestResponse<string> response = await client.ExecuteAsync<string>(request);
string responseContent = response.Data;
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal($"filename.txt:{FileContent}", responseContent);
}
[Fact]
public async Task DoWithRefitUsingByteArray()
{
var api = RestService.For<IUploadFile>("http://localhost:5000");
byte[] bytes = Encoding.UTF8.GetBytes(FileContent);
HttpResponseMessage response = await api.Upload(bytes);
string responseContent = await response.Content.ReadAsStringAsync();
Assert.Equal($"file:{FileContent}", responseContent);
}
[Fact]
public async Task DoWithRefitUsingStream()
{
var api = RestService.For<IUploadFile>("http://localhost:5000");
var stream = new MemoryStream(Encoding.UTF8.GetBytes(FileContent));
HttpResponseMessage response = await api.Upload(stream);
string responseContent = await response.Content.ReadAsStringAsync();
Assert.Equal($"file:{FileContent}", responseContent);
}
[Fact]
public async Task DoWithRefitUsingFileInfo()
{
using (var fileStream = new FileStream("filename.txt", FileMode.Create))
{
await fileStream.WriteAsync(Encoding.UTF8.GetBytes(FileContent));
}
var api = RestService.For<IUploadFile>("http://localhost:5000");
var fileInfo = new FileInfo("filename.txt");
HttpResponseMessage response = await api.Upload(fileInfo);
string responseContent = await response.Content.ReadAsStringAsync();
Assert.Equal($"filename.txt:{FileContent}", responseContent);
}
[Fact]
public async Task DoWithRefitUsingByteArrayPart()
{
var api = RestService.For<IUploadFile>("http://localhost:5000");
var bytes = new ByteArrayPart(Encoding.UTF8.GetBytes(FileContent), "filename.txt");
HttpResponseMessage response = await api.Upload(bytes);
string responseContent = await response.Content.ReadAsStringAsync();
Assert.Equal($"filename.txt:{FileContent}", responseContent);
}
[Fact]
public async Task DoWithRefitUsingStreamPart()
{
var api = RestService.For<IUploadFile>("http://localhost:5000");
var stream = new MemoryStream(Encoding.UTF8.GetBytes(FileContent));
HttpResponseMessage response = await api.Upload(new StreamPart(stream, "filename.txt"));
string responseContent = await response.Content.ReadAsStringAsync();
Assert.Equal($"filename.txt:{FileContent}", responseContent);
}
[Fact]
public async Task DoWithRefitUsingFileInfoPart()
{
using (var fileStream = new FileStream("filename2.txt", FileMode.Create))
{
await fileStream.WriteAsync(Encoding.UTF8.GetBytes(FileContent));
}
var api = RestService.For<IUploadFile>("http://localhost:5000");
var fileInfo = new FileInfoPart(new FileInfo("filename2.txt"), "filename.txt");
HttpResponseMessage response = await api.Upload(fileInfo);
string responseContent = await response.Content.ReadAsStringAsync();
Assert.Equal($"filename.txt:{FileContent}", responseContent);
}
}
}