Repository navigation
How to test multipart with SSE response? #2358
|
As previously asked in #2354 I now need to adjust the response not only to capture the request but also mock the response. |
Replies: 1 comment 1 reply
|
var parts = new Dictionary<string, string>();
using var http = new StubHttp
{
{ Route.Post("/upload"), Reply.From(async request =>
{
foreach (var part in (MultipartFormDataContent)request.Content!)
parts[part.Headers.ContentDisposition!.Name!] = await part.ReadAsStringAsync();
const string sse =
"data: {\"id\":1,\"name\":\"Ada\"}\n\n" +
"data: {\"id\":2,\"name\":\"Grace\"}\n\n";
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(sse, Encoding.UTF8, "text/event-stream"),
};
}) },
};
http.RequestCapture = RequestCapture.None;
var api = http.CreateClient<IUploadApi>("https://api.test");
var people = new List<Person>();
await foreach (var p in api.Upload(new StreamPart(stream, "a.txt", "text/plain"), "my file"))
people.Add(p);
Assert.Equal("my file", parts["description"]);
Assert.Equal(2, people.Count);Here Each event is a |
Reply.Fromcan return anyHttpResponseMessage. Refit chooses the stream framing from the responseContent-Typealone, so a body with media typetext/event-streamis parsed as SSE.Reply.ServerSentEvents(...)andStreamSourcecan't be combined with a responder, so write the SSE text yourself: