Skip to content

Commit

Permalink
Add unit test for writer
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Liu committed Jun 15, 2013
1 parent 42b5814 commit bf23845
Show file tree
Hide file tree
Showing 18 changed files with 217 additions and 60 deletions.
44 changes: 44 additions & 0 deletions Radish.IntegrationTests/HttpServerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using FluentAssertions;
using NUnit.Framework;

namespace Radish.IntegrationTests
{
public class HttpServerTests
{
[Test]
public void Handle_when_matched_request_should_reponse_setting_content()
{
// Assert
var httpServer = new HttpServer();
httpServer.When(request => request.Uri.Is("/home/index"))
.Then(response => response.Text("hello"));

var httpEngine = new HttpServerEngine(httpServer, 9000);
httpEngine.Start();

// Act
var result = Http.Get("http://localhost:9000/home/index").GetContent();
httpEngine.Stop();

// Assert
result.Should().Be("hello");
}

[Test]
public void Handle_when_request_not_matched_should_return_404()
{
// Assert
var httpServer = new HttpServer();

var httpEngine = new HttpServerEngine(httpServer, 9000);
httpEngine.Start();

// Act
var result = Http.Get("http://localhost:9000/home/index").GetContent();
httpEngine.Stop();

// Assert
result.Should().Be(string.Empty);
}
}
}
16 changes: 16 additions & 0 deletions Radish.IntegrationTests/Radish.IntegrationTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="FluentAssertions">
<HintPath>..\packages\FluentAssertions.2.0.1\lib\net45\FluentAssertions.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -39,8 +45,18 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="HttpServerTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Radish\Radish.csproj">
<Project>{c1a8978b-1629-4e6e-ab14-53fc1926802c}</Project>
<Name>Radish</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
13 changes: 9 additions & 4 deletions Radish.IntegrationTests/Radish.IntegrationTests.ncrunchproject
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
<PreventSigningOfAssembly>false</PreventSigningOfAssembly>
<AnalyseExecutionTimes>true</AnalyseExecutionTimes>
<IncludeStaticReferencesInWorkspace>true</IncludeStaticReferencesInWorkspace>
<DefaultTestTimeout>60000</DefaultTestTimeout>
<UseBuildConfiguration />
<UseBuildPlatform />
<ProxyProcessPath />
<DefaultTestTimeout>20000</DefaultTestTimeout>
<UseBuildConfiguration></UseBuildConfiguration>
<UseBuildPlatform></UseBuildPlatform>
<ProxyProcessPath></ProxyProcessPath>
<UseCPUArchitecture>AutoDetect</UseCPUArchitecture>
<MSTestThreadApartmentState>STA</MSTestThreadApartmentState>
<BuildProcessArchitecture>x86</BuildProcessArchitecture>
<IgnoredTests>
<RegexTestSelector>
<RegularExpression>.*</RegularExpression>
</RegexTestSelector>
</IgnoredTests>
</ProjectConfiguration>
5 changes: 5 additions & 0 deletions Radish.IntegrationTests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FluentAssertions" version="2.0.1" targetFramework="net45" />
<package id="NUnit" version="2.6.2" targetFramework="net45" />
</packages>
66 changes: 36 additions & 30 deletions Radish.UnitTests/Matchers/EqualRequestMatcherTests.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
namespace Radish.UnitTests.Matchers
using NSubstitute;
using NUnit.Framework;

namespace Radish.UnitTests.Matchers
{
public class EqualRequestMatcherTests
{
// [Test]
// public void Match_for_are_equal()
// {
// // Arrange
// var extractor = Substitute.For<IRequestExtractor>();
// var matcher = new EqualRequestMatcher(extractor, "/home/index");
// extractor.Extract(null).ReturnsForAnyArgs("/home/index");
//
// // Act
// var result = matcher.Match(null);
//
// // Assert
// Assert.True(result);
// }
//
// [Test]
// public void IsMatch_for_uri()
// {
// // Arrange
// var extractor = Substitute.For<IRequestExtractor>();
// var matcher = new EqualRequestMatcher(extractor, "/home/index");
// extractor.Extract(null).ReturnsForAnyArgs("invalid");
//
// // Act
// var result = matcher.Match(null);
//
// // Assert
// Assert.False(result);
// }
[Test]
public void Match_for_are_equal()
{
// Arrange
var extractor = Substitute.For<IRequestExtractor>();
var request = Substitute.For<IHttpRequest>();
extractor.Extract(request).ReturnsForAnyArgs("/home/index");
var matcher = new EqualRequestMatcher(extractor, "/home/index");

// Act
var result = matcher.Match(request);

// Assert
Assert.True(result);
}

[Test]
public void IsMatch_for_uri()
{
// Arrange
var extractor = Substitute.For<IRequestExtractor>();
var matcher = new EqualRequestMatcher(extractor, "/home/index");
var request = Substitute.For<IHttpRequest>();

extractor.Extract(request).ReturnsForAnyArgs("invalid");

// Act
var result = matcher.Match(request);

// Assert
Assert.False(result);
}
}
}
4 changes: 2 additions & 2 deletions Radish.UnitTests/Matchers/OrRequestMatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void Match_when_matchers_has_any_matched_should_return_true()
}

[Test]
public void OperatorOr_when_left_is_OrRequestMatcher_should_not_append_matcher_to_left()
public void OperatorOr_when_left_is_OrRequestMatcher_should_append_matcher_to_the_OrRequestMatcher()
{
// Assert
var matcher1 = Substitute.For<AbstractRequestMatcher>();
Expand All @@ -67,7 +67,7 @@ public void OperatorOr_when_left_is_OrRequestMatcher_should_not_append_matcher_t
}

[Test]
public void OperatorOr_when_right_is_OrRequestMatcher_should_not_append_matcher_to_right()
public void OperatorOr_when_right_is_OrRequestMatcher_should_append_matcher_to_the_OrRequestMatcher()
{
// Assert
var matcher1 = Substitute.For<AbstractRequestMatcher>();
Expand Down
7 changes: 7 additions & 0 deletions Radish.UnitTests/Radish.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SettingTest.cs" />
<Compile Include="UrlTest.cs" />
<Compile Include="Writers\ResponseFileWriterTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand All @@ -74,6 +75,12 @@
<Name>Radish</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Content Include="test.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
10 changes: 6 additions & 4 deletions Radish.UnitTests/Radish.UnitTests.ncrunchproject
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
<PreventSigningOfAssembly>false</PreventSigningOfAssembly>
<AnalyseExecutionTimes>true</AnalyseExecutionTimes>
<IncludeStaticReferencesInWorkspace>true</IncludeStaticReferencesInWorkspace>
<DefaultTestTimeout>60000</DefaultTestTimeout>
<UseBuildConfiguration />
<UseBuildPlatform />
<ProxyProcessPath />
<DefaultTestTimeout>10000</DefaultTestTimeout>
<UseBuildConfiguration></UseBuildConfiguration>
<UseBuildPlatform></UseBuildPlatform>
<ProxyProcessPath></ProxyProcessPath>
<UseCPUArchitecture>AutoDetect</UseCPUArchitecture>
<MSTestThreadApartmentState>STA</MSTestThreadApartmentState>
<BuildProcessArchitecture>x86</BuildProcessArchitecture>
</ProjectConfiguration>
28 changes: 28 additions & 0 deletions Radish.UnitTests/Writers/ResponseFileWriterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.IO;
using System.Text;
using FluentAssertions;
using NSubstitute;
using NUnit.Framework;
using Radish.Writers;

namespace Radish.UnitTests.Writers
{
public class FileResponseWriterTests
{
[Test]
public void Write()
{
// Arrange
var responseStream = new MemoryStream();
var response = Substitute.For<IHttpResponse>();
response.OutputStream.Returns(responseStream);
var fileWriter = new FileResponseWriter("test.txt");

// Act
fileWriter.Write(response);

// Assert
Encoding.UTF8.GetString(responseStream.ToArray()).Should().Be("hello");
}
}
}
1 change: 1 addition & 0 deletions Radish.UnitTests/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
Binary file modified Radish.v11.suo
Binary file not shown.
1 change: 0 additions & 1 deletion Radish/Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public static object Post(string url, string data)
catch (WebException exc)
{
response = (HttpWebResponse)exc.Response;

}
if (response != null)
{
Expand Down
4 changes: 4 additions & 0 deletions Radish/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ public void Then(Action<ResponseHandler> responseAction)

public class HttpServer : HttpHandlerSetting
{
private readonly ResponseHandler _pageNotFoundHandler;

public HttpServer()
{
_pageNotFoundHandler = new ResponseHandler();

_settings = new List<Setting>();
}

Expand All @@ -62,6 +65,7 @@ internal void Proccess(IHttpContext context)
return;
}
}

}
}
}
4 changes: 3 additions & 1 deletion Radish/Radish.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@
<Compile Include="RequestMatcherSetting.cs" />
<Compile Include="RequestSetting.cs" />
<Compile Include="Extractors\UriRequestExtractor.cs" />
<Compile Include="ResponseFileWriter.cs" />
<Compile Include="Writers\FileResponseWriter.cs" />
<Compile Include="ResponseHeaderWriter.cs" />
<Compile Include="ResponseSetting.cs" />
<Compile Include="Writers\ResponseStatusCodeWriter.cs" />
<Compile Include="Writers\ResponseTextWriter.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down
9 changes: 8 additions & 1 deletion Radish/ResponseSetting.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using Radish.Writers;

namespace Radish
{
Expand All @@ -16,7 +18,7 @@ public ResponseHandler()

public ResponseHandler File(string file)
{
_writers.Add(new ResponseFileWriter(file));
_writers.Add(new FileResponseWriter(file));
return this;
}

Expand All @@ -39,5 +41,10 @@ public ResponseHandler Text(string text)
_writers.Add(new ResponseTextWriter(text, Encoding.UTF8));
return this;
}

public void Status(int statusCode)
{
throw new NotImplementedException();
}
}
}
28 changes: 28 additions & 0 deletions Radish/Writers/FileResponseWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.IO;

namespace Radish.Writers
{
public class FileResponseWriter : IResponseWriter
{
private readonly string _file;

public FileResponseWriter(string file)
{
_file = file;
}

public void Write(IHttpResponse response)
{
using (var stream = File.Open(_file, FileMode.Open))
{
var data = (byte)stream.ReadByte();
while (data != -1)
{
response.OutputStream.WriteByte(data);
data = (byte)stream.ReadByte();
}
}
}
}
}
19 changes: 19 additions & 0 deletions Radish/Writers/ResponseStatusCodeWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace Radish.Writers
{
public class ResponseStatusCodeWriter : IResponseWriter
{
private readonly int _statusCode;

public ResponseStatusCodeWriter(int statusCode)
{
_statusCode = statusCode;
}

public void Write(IHttpResponse response)
{
throw new NotImplementedException();
}
}
}
Loading

0 comments on commit bf23845

Please sign in to comment.