Skip to content

Commit

Permalink
Merge branch 'master' of [email protected]:canuckotter/SolrNet.git
Browse files Browse the repository at this point in the history
  • Loading branch information
mausch committed Mar 31, 2014
2 parents 8c77e4e + d4f70c7 commit 8286c6c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
<Project>{931CEB14-556F-4893-A8D9-C7C6DACA444F}</Project>
<Name>AutofacContrib.SolrNet</Name>
</ProjectReference>
<ProjectReference Include="..\HttpWebAdapters\HttpWebAdapters.csproj">
<Project>{AE7D2A46-3F67-4986-B04B-7DCE79A549A5}</Project>
<Name>HttpWebAdapters</Name>
</ProjectReference>
<ProjectReference Include="..\SolrNet.Tests\SolrNet.Tests.csproj">
<Project>{F3FE6EF5-CF5C-4461-8691-4A498A463FD5}</Project>
<Name>SolrNet.Tests</Name>
Expand Down
37 changes: 37 additions & 0 deletions AutofacContrib.SolrNet.Tests/AutofacFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using Autofac;
using HttpWebAdapters;
using MbUnit.Framework;
using SolrNet;
using SolrNet.Impl;
using SolrNet.Tests.Mocks;
using Mocks = SolrNet.Tests.Mocks;

namespace AutofacContrib.SolrNet.Tests {
[TestFixture]
Expand All @@ -35,6 +40,38 @@ public void ReplaceMapper() {
Assert.AreSame(mapper, m);
}

[Test]
public void ReplaceHttpWebRequestFactory()
{
var builder = new ContainerBuilder();
var getResponseCalls = 0;
var response = new Mocks.HttpWebResponse
{
dispose = () => { },
headers = () => new WebHeaderCollection {
{HttpResponseHeader.ETag, "123"},
},
getResponseStream = () =>
// If we don't give back at least the basic XML, we get an XmlParseException
new MemoryStream(Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?><response />")),
};
IHttpWebRequestFactory factory = new Mocks.HttpWebRequestFactory {
create = _ => new Mocks.HttpWebRequest {
getResponse = () => {
getResponseCalls++;
return response;
},
Headers = new WebHeaderCollection(),
},
};
builder.RegisterModule(new SolrNetModule("http://localhost:8983/solr") { HttpWebRequestFactory = factory });
var container = builder.Build();
var operations = container.Resolve<ISolrOperations<Dictionary<string, object>>>();
var results = operations.Query(new SolrQuery("q:*"));
Assert.IsNotNull(results);
Assert.AreEqual(1, getResponseCalls);
}

[Test]
public void ResolveSolrOperations() {
var builder = new ContainerBuilder();
Expand Down
4 changes: 4 additions & 0 deletions AutofacContrib.SolrNet/AutofacContrib.SolrNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HttpWebAdapters\HttpWebAdapters.csproj">
<Project>{AE7D2A46-3F67-4986-B04B-7DCE79A549A5}</Project>
<Name>HttpWebAdapters</Name>
</ProjectReference>
<ProjectReference Include="..\SolrNet\SolrNet.csproj">
<Project>{CEEB8690-3E08-4440-B647-787A58E71CFA}</Project>
<Name>SolrNet</Name>
Expand Down
13 changes: 12 additions & 1 deletion AutofacContrib.SolrNet/SolrNetModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Autofac;
using Autofac.Core;
using AutofacContrib.SolrNet.Config;
using HttpWebAdapters;
using SolrNet;
using SolrNet.Impl;
using SolrNet.Impl.DocumentPropertyVisitors;
Expand Down Expand Up @@ -65,6 +66,11 @@ public SolrNetModule(string serverUrl) {
/// </summary>
public IReadOnlyMappingManager Mapper { get; set; }

/// <summary>
/// Optional override to provide a different <see cref="IHttpWebRequestFactory"/>.
/// </summary>
public IHttpWebRequestFactory HttpWebRequestFactory { get; set; }

private void RegisterCommonComponents(ContainerBuilder builder) {
var mapper = Mapper ?? new MemoizingMappingManager(new AttributesMappingManager());
builder.RegisterInstance(mapper).As<IReadOnlyMappingManager>();
Expand Down Expand Up @@ -102,7 +108,12 @@ private void RegisterCommonComponents(ContainerBuilder builder) {

private void RegisterSingleCore(ContainerBuilder builder) {
RegisterCommonComponents(builder);
builder.RegisterInstance(new SolrConnection(ServerUrl)).As<ISolrConnection>();

SolrConnection solrConnection = new SolrConnection(ServerUrl);
if (HttpWebRequestFactory != null) {
solrConnection.HttpWebRequestFactory = HttpWebRequestFactory;
}
builder.RegisterInstance(solrConnection).As<ISolrConnection>();

builder.RegisterGeneric(typeof (SolrBasicServer<>))
.As(typeof (ISolrBasicOperations<>), typeof (ISolrBasicReadOnlyOperations<>))
Expand Down

0 comments on commit 8286c6c

Please sign in to comment.