Skip to content

Commit

Permalink
Added *very simple* AspNet handler and sample. WARNING: The AspNet st…
Browse files Browse the repository at this point in the history
…uff is barely specced because HttpRequest/HttpResponse don't like to be modified and I haven't gotten around to mocking them.
  • Loading branch information
remi committed Dec 12, 2010
1 parent ef22b6f commit 2b90626
Show file tree
Hide file tree
Showing 21 changed files with 736 additions and 375 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -6,4 +6,7 @@ TestResult.xml
examples/**/*.dll
examples/**/*.exe
examples/**/*.cgi
examples/**/*/*.dll
examples/**/*/*.exe
examples/**/*/*.cgi
output
25 changes: 25 additions & 0 deletions examples/simple-aspnet-application/AspNetApp.cs
@@ -0,0 +1,25 @@
using System;
using System.Web;
using System.Collections.Generic;
using Owin;

namespace AspNetAppSample {
public class MyApp : AspNetApplication, IHttpHandler {
public override IResponse Call(IRequest rawRequest) {
Request request = new Request(rawRequest);
Response response = new Response();

response.ContentType = "text/plain";
response.Write("{0} {1}\n\n", request.Method, request.Uri);

foreach (KeyValuePair<string,string> param in request.Params)
response.Write("Param {0} = {1}\n", param.Key, param.Value);

foreach (KeyValuePair<string,IEnumerable<string>> header in request.Headers)
foreach (string value in header.Value)
response.Write("Header {0} = {1}\n", header.Key, value);

return response;
}
}
}
8 changes: 8 additions & 0 deletions examples/simple-aspnet-application/Web.config
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="*" type="AspNetAppSample.MyApp, AspNetApp" />
</httpHandlers>
</system.web>
</configuration>
Binary file not shown.
26 changes: 26 additions & 0 deletions examples/simple-aspnet-application/WebDev.WebServer20.sln
@@ -0,0 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{911E67C6-3D85-4FCE-B560-20A9C3E3FF48}") = "WebDev.WebServer20", "WebDev.WebServer20.EXE", "{55ED36A7-C0E7-4308-BC8C-D54BBD9C43B4}"
ProjectSection(DebuggerProjectSystem) = preProject
PortSupplier = 00000000-0000-0000-0000-000000000000
Executable = C:\Users\rtaylor\projects\remi\Owin.Common\examples\simple-aspnet-application\WebDev.WebServer20.EXE
RemoteMachine = EMWL089
StartingDirectory = C:\Users\rtaylor\projects\remi\Owin.Common\examples\simple-aspnet-application
Environment = Default
LaunchingEngine = 00000000-0000-0000-0000-000000000000
LaunchSQLEngine = No
AttachLaunchAction = No
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{55ED36A7-C0E7-4308-BC8C-D54BBD9C43B4}.Debug|x86.ActiveCfg = Debug|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions examples/simple-aspnet-application/build.bat
@@ -0,0 +1,6 @@
@echo off
mkdir Bin
xcopy ..\..\bin\Debug\*.dll .
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /target:library AspNetApp.cs /r:owin.dll /r:Owin.Common.dll /r:Owin.Handlers.AspNet.dll
xcopy *.dll Bin\
pause
2 changes: 2 additions & 0 deletions examples/simple-aspnet-application/run-cassini.bat
@@ -0,0 +1,2 @@
WebDev.WebServer20.EXE /port:8080 /path:"C:\Users\rtaylor\projects\remi\Owin.Common\examples\simple-aspnet-application"
pause
2 changes: 1 addition & 1 deletion run-specs.bat
Expand Up @@ -4,4 +4,4 @@ SET NUNIT_CONSOLE = tools\nunit-console.exe

RMDIR /Q bin

MSBuild && tools\nunit-color-console.exe /labels bin\Debug\Owin.Common.Specs.dll
MSBuild && tools\nunit-color-console.exe /labels bin\Debug\Owin.Common.Specs.dll bin\Debug\Owin.Handlers.Cgi.Specs.dll bin\Debug\Owin.Handlers.AspNet.Specs.dll
157 changes: 157 additions & 0 deletions spec/Handlers/AspNet/AspNetRequestSpec.cs
@@ -0,0 +1,157 @@
using System;
using System.Web;
using NUnit.Framework;

namespace Owin.Handlers.Specs {

[TestFixture]
public class AspNetRequestSpec {

AspNetRequest request;

HttpRequest GetRequest() {
return GetRequest("/");
}
HttpRequest GetRequest(string uri) {
return GetRequest("GET", uri);
}
HttpRequest GetRequest(string method, string uri) {
return GetRequest(method, uri, "");
}
HttpRequest GetRequest(string method, string uri, string queryStrings) {
string url = uri;
if (uri.StartsWith("/"))
url = "http://localhost" + uri;
if (queryStrings != "")
url += "?" + queryStrings;
HttpRequest request = new HttpRequest("", url, queryStrings);
request.RequestType = method;
return request;
}

[SetUp]
public void Before() { request = null; }

[TearDown]
public void After() {
if (request != null)
Owin.Lint.Validate(request);
}

[TestFixture]
public class Uri_Method_and_QueryStrings : AspNetRequestSpec {

[Test]
public void Method_gets_set_using_Request_RequestType() {
request = new AspNetRequest(GetRequest());
Assert.That(request.Method, Is.EqualTo("GET"));

request = new AspNetRequest(GetRequest("POST", "/"));
Assert.That(request.Method, Is.EqualTo("POST"));
}

[Test]
public void Uri_gets_set_using_Request_Path() {
request = new AspNetRequest(GetRequest("/"));
Assert.That(request.Uri, Is.EqualTo("/"));

request = new AspNetRequest(GetRequest("/foo/bar"));
Assert.That(request.Uri, Is.EqualTo("/foo/bar"));
}

[Test]
public void Uri_includes_query_strings_from_QueryString() {
request = new AspNetRequest(GetRequest("GET", "/", ""));
Assert.That(request.Uri, Is.EqualTo("/"));

request = new AspNetRequest(GetRequest("GET", "/", "foo=bar"));
Assert.That(request.Uri, Is.EqualTo("/?foo=bar"));
}

[Test]
public void Uri_gets_QueryString_from_QueryString() {
request = new AspNetRequest(GetRequest("GET", "/", ""));
Assert.That(request.QueryString, Is.EqualTo(""));

request = new AspNetRequest(GetRequest("GET", "/", "foo=bar"));
Assert.That(request.QueryString, Is.EqualTo("foo=bar"));
}
}

[TestFixture]
public class Items : AspNetRequestSpec {

[Test]
public void owin_base_path_gets_set_to_Request_ApplicationPath() {
HttpRequest native = GetRequest();
Assert.That(native.ApplicationPath, Is.Null); // it returns null and we can't change it

request = new AspNetRequest(native);
Assert.That(request.BasePath, Is.EqualTo(""));
}

[Test]
public void owin_server_name_gets_set_to_Request_Url_Host() {
request = new AspNetRequest(GetRequest("http://localhost/"));
Assert.That(request.Host, Is.EqualTo("localhost"));

request = new AspNetRequest(GetRequest("http://google.com/"));
Assert.That(request.Host, Is.EqualTo("google.com"));
}

[Test]
public void owin_server_port_gets_set_to_Request_Url_Port() {
request = new AspNetRequest(GetRequest("http://localhost/"));
Assert.That(request.Port, Is.EqualTo(80));

request = new AspNetRequest(GetRequest("http://localhost:1234/"));
Assert.That(request.Port, Is.EqualTo(1234));
}

[Test]
public void owin_request_protocol_gets_set_to_HTTP_1_1() {
request = new AspNetRequest(GetRequest());
Assert.That(request.Protocol, Is.EqualTo("HTTP/1.1")); // not sure if we can detect this - just use 1.1
}

[Test]
public void owin_url_scheme_gets_set_using_scheme_seen_in_Request_Url_Scheme() {
request = new AspNetRequest(GetRequest("http://localhost/"));
Assert.That(request.Scheme, Is.EqualTo("http"));

request = new AspNetRequest(GetRequest("https://localhost/"));
Assert.That(request.Scheme, Is.EqualTo("https"));
}

[Test]
public void owin_remote_endpoint_gets_set_using_Request_UserHostAddress_and_Port() {
HttpRequest native = GetRequest(); // unfortunately, we can't set this :/
Assert.That(native.UserHostAddress, Is.Null);

// if it's null, we'll do 0.0.0.0:80 ... cause, why not?
request = new AspNetRequest(native);
Assert.That(request.IPEndPoint.ToString(), Is.EqualTo("0.0.0.0:80"));

// if we change the port, it should change the port in the end point
request = new AspNetRequest(GetRequest("http://localhost:1234/foo"));
Assert.That(request.IPEndPoint.ToString(), Is.EqualTo("0.0.0.0:1234"));
}
}

[TestFixture]
public class Headers : AspNetRequestSpec {

[Test][Ignore("If you try to modify HttpRequest.Headers it throws a PlatformNotSupportedException")]
public void All_environment_variables_become_headers() {
}
}

[TestFixture]
public class Body : AspNetRequestSpec {

[Test][Ignore("If you try to modify HttpRequest.Form it throws NotSupportedException : Collection is read-only")]
public void Can_get_posted_params() {
}
}
}
}
32 changes: 32 additions & 0 deletions spec/Handlers/AspNet/AspNetResponseSpec.cs
@@ -0,0 +1,32 @@
using System;
using System.IO;
using System.Web;
using System.Text;
using System.Collections.Generic;
using NUnit.Framework;

namespace Owin.Handlers.Specs {

[TestFixture]
public class AspNetResponseSpec {

[Test][Ignore("Trying to get HttpResponse.Headers blows up with PlatformNotSupportedException : This operation requires IIS integrated pipeline mode")]
public void Can_convert_IResponse_into_HttpResponse_given_an_HttpResponse_reference() {
TextWriter body = new StringWriter();
HttpResponse response = new HttpResponse(body);

// check Response defaults
Assert.That(response.Status, Is.EqualTo("200 OK"));
Assert.That(body.ToString(), Is.EqualTo(""));
Assert.That(response.Headers.Count, Is.EqualTo(0));

AspNet.WriteResponse(response, new Response(404, "Oops, not found!", new Dictionary<string,string>{{"content-type","text/plains"}}));

// check that WriteResponse() updated the response
Assert.That(response.Status, Is.EqualTo("404 NotFound"));
Assert.That(body.ToString(), Is.EqualTo("Oops, not found!"));
Assert.That(response.Headers.Count, Is.EqualTo(1));
Assert.That(response.Headers["content-type"], Is.EqualTo("text/plain"));
}
}
}
9 changes: 0 additions & 9 deletions spec/Handlers/AspNet/Class1.cs

This file was deleted.

4 changes: 3 additions & 1 deletion spec/Handlers/AspNet/Owin.Handlers.AspNet.Specs.csproj
Expand Up @@ -40,10 +40,12 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="AspNetRequestSpec.cs" />
<Compile Include="AspNetResponseSpec.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Common\Owin.Common.csproj">
Expand Down

0 comments on commit 2b90626

Please sign in to comment.