Hass
Hass
Explore posts from servers
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
public static Mock<HttpRequestData> HttpRequestDataMock(string data, HttpStatusCode httpStatusCode = HttpStatusCode.OK)
{
Mock<FunctionContext> functionContextMock = new();
HttpHeadersCollection headers = new() { { "Authorization", new List<string?> { "Bearer token" } } };
headers.Add("content-type", "application/json");

Mock<HttpRequestData> httpRequestDataMock = new(functionContextMock.Object);
httpRequestDataMock.Setup(r => r.Headers).Returns(headers);
httpRequestDataMock.Setup(r => r.Body).Returns(data.CreateMemoryStream());
httpRequestDataMock.Setup(r => r.CreateResponse()).Returns(() =>
{
Mock<HttpResponseData> httpResponseData = new(functionContextMock.Object);
httpResponseData.SetupProperty(s => s.Headers, headers);
httpResponseData.SetupProperty(s => httpStatusCode);
httpResponseData.SetupProperty(s => s.Body, new MemoryStream());
return httpResponseData.Object;
});

return httpRequestDataMock;
}
public static Mock<HttpRequestData> HttpRequestDataMock(string data, HttpStatusCode httpStatusCode = HttpStatusCode.OK)
{
Mock<FunctionContext> functionContextMock = new();
HttpHeadersCollection headers = new() { { "Authorization", new List<string?> { "Bearer token" } } };
headers.Add("content-type", "application/json");

Mock<HttpRequestData> httpRequestDataMock = new(functionContextMock.Object);
httpRequestDataMock.Setup(r => r.Headers).Returns(headers);
httpRequestDataMock.Setup(r => r.Body).Returns(data.CreateMemoryStream());
httpRequestDataMock.Setup(r => r.CreateResponse()).Returns(() =>
{
Mock<HttpResponseData> httpResponseData = new(functionContextMock.Object);
httpResponseData.SetupProperty(s => s.Headers, headers);
httpResponseData.SetupProperty(s => httpStatusCode);
httpResponseData.SetupProperty(s => s.Body, new MemoryStream());
return httpResponseData.Object;
});

return httpRequestDataMock;
}
Mock<HttpRequestData> request = HttpRequestDataMock(validPayload);
using StreamReader reader = new(request.Object.Body, encoding: System.Text.Encoding.UTF8, leaveOpen: true);
string bodyResult = await reader.ReadToEndAsync();
Mock<HttpRequestData> request = HttpRequestDataMock(validPayload);
using StreamReader reader = new(request.Object.Body, encoding: System.Text.Encoding.UTF8, leaveOpen: true);
string bodyResult = await reader.ReadToEndAsync();
The validPayload is the valid json
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
one minute
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
I guess I can
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
I tried expliciting the content-type for the HttpRequestData that is created, but same result
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
I checked and I have the same packages (different version tho, as they are more recent)
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
the same as the hardcoded string in the code
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
what is sent? {"schemaId":"azureMonitorCommonAlertSchema","data":{...}}
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
and the date
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
Server: kestrel
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
Transfer-Encoding: chunked
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
in the headers stuff?
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
I'm using a premade function
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
in the payload? lemme check
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
I think it's plausible, but wouldn't it also break incoming jsons? I tested it using postman and it was fine
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
but to do so, I have to convert to a Stream, as it does not accept a string
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
So what I'm (trying) to do is to create a HttpRequestData and set its Body property as the json
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
I'm testing an azure function, so the way they implemented it here is by expecting a HttpRequestData, instead of a HttpRequest
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
using StreamReader reader = new(request.Object.Body, encoding: System.Text.Encoding.UTF8, leaveOpen: true);
string bodyResult = await reader.ReadToEndAsync();
using StreamReader reader = new(request.Object.Body, encoding: System.Text.Encoding.UTF8, leaveOpen: true);
string bodyResult = await reader.ReadToEndAsync();
the code
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
the result is the broken string
54 replies
CC#
Created by Hass on 3/7/2025 in #help
Converting json to MemoryStream and back breaks the json
you mean like string result = await reader.ReadToEndAsync()?
54 replies