public void TestSerializeRequest_VersionMissing_ShouldSerializeRequest() { string input = "POST /modules/testModule/sign?api-version=2018-06-28 HTTP/1.1\r\nHost: localhost:8081\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: 100\r\n\r\n"; HttpRequestMessage request = new HttpRequestMessage(); request.RequestUri = new Uri("http://localhost:8081/modules/testModule/sign?api-version=2018-06-28", UriKind.Absolute); request.Method = HttpMethod.Post; request.Content = new ByteArrayContent(Encoding.UTF8.GetBytes("test")); request.Content.Headers.TryAddWithoutValidation("content-type", "application/json"); request.Content.Headers.TryAddWithoutValidation("content-length", "100"); byte[] httpRequestData = HttpRequestResponseSerializer.SerializeRequest(request); string requestString = Encoding.ASCII.GetString(httpRequestData); // assert var newLines = new[] { '\r', '\n' }; var actual = requestString.Split(newLines, StringSplitOptions.RemoveEmptyEntries); var expected = input.Split(newLines, StringSplitOptions.RemoveEmptyEntries); actual.Length.Should().Be(expected.Length); for (int i = 0; i < actual.Length; ++i) { actual[i].Should().BeEquivalentTo(expected[i]); } }
public void TestSerializeRequest_RequestUriIsNull_ShouldThrowArgumentNullException() { HttpRequestMessage request = new HttpRequestMessage(); request.Method = HttpMethod.Post; request.Content = new ByteArrayContent(Encoding.UTF8.GetBytes("test")); request.Content.Headers.TryAddWithoutValidation("content-type", "application/json"); TestAssert.Throws <ArgumentNullException>(() => HttpRequestResponseSerializer.SerializeRequest(request)); }
public void TestSerializeRequest_ContentIsNull_ShouldSerializeRequest() { string expected = "GET /modules/testModule/sign?api-version=2018-06-28 HTTP/1.1\r\nHost: localhost:8081\r\nConnection: close\r\n\r\n"; HttpRequestMessage request = new HttpRequestMessage(); request.RequestUri = new Uri("http://localhost:8081/modules/testModule/sign?api-version=2018-06-28", UriKind.Absolute); request.Method = HttpMethod.Get; byte[] httpRequestData = HttpRequestResponseSerializer.SerializeRequest(request); string actual = Encoding.ASCII.GetString(httpRequestData); Assert.AreEqual(expected, actual, true, CultureInfo.InvariantCulture); }
public void TestSerializeRequest_ContentLengthMissing_ShouldSerializeRequest() { string expected = "POST /modules/testModule/sign?api-version=2018-06-28 HTTP/1.1\r\nHost: localhost:8081\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: 4\r\n\r\n"; HttpRequestMessage request = new HttpRequestMessage(); request.RequestUri = new Uri("http://localhost:8081/modules/testModule/sign?api-version=2018-06-28", UriKind.Absolute); request.Method = HttpMethod.Post; request.Content = new ByteArrayContent(Encoding.UTF8.GetBytes("test")); request.Content.Headers.TryAddWithoutValidation("content-type", "application/json"); byte[] httpRequestData = HttpRequestResponseSerializer.SerializeRequest(request); string actual = Encoding.ASCII.GetString(httpRequestData); Assert.AreEqual(expected, actual, true, CultureInfo.InvariantCulture); }
static async Task <string> Signer(string base64String) { if (string.IsNullOrEmpty(base64String)) { return(null); } try { string IOTEDGE_MODULEID = Environment.GetEnvironmentVariable("IOTEDGE_MODULEID"); string IOTEDGE_MODULEGENERATIONID = Environment.GetEnvironmentVariable("IOTEDGE_MODULEGENERATIONID"); string IOTEDGE_WORKLOADURI = Environment.GetEnvironmentVariable("IOTEDGE_WORKLOADURI").Replace("unix://", ""); UnixEndPoint unixEndpoint = new UnixEndPoint(IOTEDGE_WORKLOADURI); Socket unixSocket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP); unixSocket.Connect(unixEndpoint); string httpRequestURI = $"http://localhost/modules/{IOTEDGE_MODULEID}/genid/{IOTEDGE_MODULEGENERATIONID}/sign?api-version=2018-06-28"; string requestContent = "{\"keyId\": \"primary\", \"algo\": \"HMACSHA256\", \"data\": \"" + base64String + "\"}"; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, httpRequestURI); request.Content = new StringContent(requestContent, Encoding.UTF8, "application/json"); HttpRequestResponseSerializer serializer = new HttpRequestResponseSerializer(); byte[] requestBytes = serializer.SerializeRequest(request); HttpBufferedStream stream = new HttpBufferedStream(new NetworkStream(unixSocket, true)); await stream.WriteAsync(requestBytes, 0, requestBytes.Length, CancellationToken.None).ConfigureAwait(false); if (request.Content != null) { await request.Content.CopyToAsync(stream).ConfigureAwait(false); } HttpResponseMessage response = await serializer.DeserializeResponse(stream, CancellationToken.None).ConfigureAwait(false); string responseString = await response.Content.ReadAsStringAsync(); var responseObject = Newtonsoft.Json.Linq.JObject.Parse(responseString); return(responseObject.Value <string>("digest")); } catch (System.Exception ex) { Console.WriteLine(ex.Message); return(null); } }
public void TestSerializeRequest_ContentIsNull_ShouldSerializeRequest() { string input = "GET /modules/testModule/sign?api-version=2018-06-28 HTTP/1.1\r\nHost: localhost:8081\r\nConnection: close\r\n\r\n"; HttpRequestMessage request = new HttpRequestMessage(); request.RequestUri = new Uri("http://localhost:8081/modules/testModule/sign?api-version=2018-06-28", UriKind.Absolute); request.Method = HttpMethod.Get; byte[] httpRequestData = HttpRequestResponseSerializer.SerializeRequest(request); string requestString = Encoding.ASCII.GetString(httpRequestData); // assert var newLines = new[] { '\r', '\n' }; var actual = requestString.Split(newLines, StringSplitOptions.RemoveEmptyEntries); var expected = input.Split(newLines, StringSplitOptions.RemoveEmptyEntries); actual.Length.Should().Be(expected.Length); for (int i = 0; i < actual.Length; ++i) { actual[i].Should().BeEquivalentTo(expected[i]); } }
public void TestSerializeRequest_RequestIsNull_ShouldThrowArgumentNullException() { TestAssert.Throws <ArgumentNullException>(() => HttpRequestResponseSerializer.SerializeRequest(null)); }