public void GetCoapRequest_BadMethod() { HttpRequest http = new HttpRequest("FOOBAR /foo HTTP/1.1\n" + Host); TranslationException e = Assert.Throws <TranslationException>(() => HttpTranslator.GetCoapRequest(http, "xx", true)); Assert.That(e.Message, Is.EqualTo("FOOBAR method not mapped")); }
public void GetCoapRequest_SimpleForm2() { string proxyUri = "coaps://coap.example.com/resource"; HttpRequest http = new HttpRequest("GET /hc/?target=" + proxyUri + " HTTP/1.1\n" + Host); Request req = HttpTranslator.GetCoapRequest(http, "hc/?target={+tu}", true); Assert.That(req.Method, Is.EqualTo(Method.GET)); Assert.That(req.ProxyUri.ToString(), Is.EqualTo(proxyUri)); }
public void GetCoapRequest_SimpleForm() { HttpRequest http; string proxyUri = "coap://coap.example.com/resource"; http = new HttpRequest("GET /hc/" + proxyUri + " HTTP/1.1\n" + Host); Request req = HttpTranslator.GetCoapRequest(http, "hc/{+tu}", true); Assert.That(req.Method, Is.EqualTo(Method.GET)); Assert.That(req.ProxyUri.ToString(), Is.EqualTo(proxyUri)); proxyUri = "//coap.example.com/resource"; http = new HttpRequest("GET /hc/" + proxyUri + " HTTP/1.1\n" + Host); TranslationException e = Assert.Throws <TranslationException>(() => HttpTranslator.GetCoapRequest(http, "hc/{+tu}", true)); Assert.That(e.Message, Is.EqualTo("Schema is required")); proxyUri = "coap://coap.example.com/?query=1"; http = new HttpRequest("GET /hc/" + proxyUri + " HTTP/1.1\n" + Host); req = HttpTranslator.GetCoapRequest(http, "hc/{+tu}", true); Assert.That(req.Method, Is.EqualTo(Method.GET)); Assert.That(req.ProxyUri.ToString(), Is.EqualTo(proxyUri)); proxyUri = "coap://coap.example.com/resource?query=1"; http = new HttpRequest("GET /hc/" + proxyUri + " HTTP/1.1\n" + Host); req = HttpTranslator.GetCoapRequest(http, "hc/{+tu}", true); Assert.That(req.Method, Is.EqualTo(Method.GET)); Assert.That(req.ProxyUri.ToString(), Is.EqualTo(proxyUri)); proxyUri = "coap://coap.example.com:5848/resource"; http = new HttpRequest("GET /hc/" + proxyUri + " HTTP/1.1\n" + Host); req = HttpTranslator.GetCoapRequest(http, "hc/{+tu}", true); Assert.That(req.Method, Is.EqualTo(Method.GET)); Assert.That(req.ProxyUri.ToString(), Is.EqualTo(proxyUri)); }
private CoapDotNetHttpResponse Proxy(CoapDotNetHttpRequest request, Uri coapUri) { var coapRequest = HttpTranslator.GetCoapRequest(request, Request.Url.SiteBase, true); coapRequest.URI = coapUri; //Setup response handler Response response = null; EventHandler <ResponseEventArgs> responseHandler = null; responseHandler = (_, e) => { response = e.Response; coapRequest.Respond -= responseHandler; }; coapRequest.Respond += responseHandler; //send request coapRequest.Send(); DateTime start = DateTime.Now; // ReSharper disable once LoopVariableIsNeverChangedInsideLoop while (response == null && (DateTime.Now - start).TotalSeconds < 30) { Thread.Sleep(1); } if (response == null) { return(null); } //Turn COAP response into HTTP response var httpResponse = new CoapDotNetHttpResponse { MaxAge = response.MaxAge }; HttpTranslator.GetHttpResponse(request, response, httpResponse); return(httpResponse); }
public void Process(IHttpRequest httpRequest, IHttpResponse httpResponse) { Request coapRequest = HttpTranslator.GetCoapRequest(httpRequest, _localResource, _proxyingEnabled); }