Skip to content

Commit

Permalink
Breaking change (acceptable because it didn't work before) - Change t…
Browse files Browse the repository at this point in the history
…he way we customize the HttpMessageHandler to an HttpMessageHandlerFactory
  • Loading branch information
ayende committed May 21, 2015
1 parent 2916704 commit 740ad10
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,13 @@ internal HttpJsonRequest(
owner = requestParams.Owner;
conventions = requestParams.Convention;

if (factory.httpMessageHandler != null)
recreateHandler = () => factory.httpMessageHandler;
else
{
recreateHandler = () => new WebRequestHandler
recreateHandler = factory.httpMessageHandler ?? (
() => new WebRequestHandler
{
UseDefaultCredentials = _credentials != null && _credentials.HasCredentials() == false,
Credentials = _credentials != null ? _credentials.Credentials : null,
};
}
}
);

httpClient = factory.httpClientCache.GetClient(Timeout, _credentials, recreateHandler);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public HttpClientCache HttpClientCache
get { return httpClientCache; }
}

internal readonly HttpMessageHandler httpMessageHandler;
internal readonly Func<HttpMessageHandler> httpMessageHandler;

internal readonly bool acceptGzipContent;

Expand Down Expand Up @@ -173,7 +173,7 @@ public int CurrentCacheSize
/// default ctor
/// </summary>
/// <param name="maxNumberOfCachedRequests"></param>
public HttpJsonRequestFactory(int maxNumberOfCachedRequests, HttpMessageHandler httpMessageHandler = null, bool acceptGzipContent = true)
public HttpJsonRequestFactory(int maxNumberOfCachedRequests, Func<HttpMessageHandler> httpMessageHandler = null, bool acceptGzipContent = true)
{
this.maxNumberOfCachedRequests = maxNumberOfCachedRequests;
this.httpMessageHandler = httpMessageHandler;
Expand Down
4 changes: 2 additions & 2 deletions Raven.Client.Lightweight/Document/DocumentStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public IDocumentStore Initialize(bool ensureDatabaseExists)

AssertValidConfiguration();

jsonRequestFactory = new HttpJsonRequestFactory(MaxNumberOfCachedRequests, HttpMessageHandler, Conventions.AcceptGzipContent);
jsonRequestFactory = new HttpJsonRequestFactory(MaxNumberOfCachedRequests, HttpMessageHandlerFactory, Conventions.AcceptGzipContent);

try
{
Expand Down Expand Up @@ -821,7 +821,7 @@ public int MaxNumberOfCachedRequests
}
}

public HttpMessageHandler HttpMessageHandler { get; set; }
public Func<HttpMessageHandler> HttpMessageHandlerFactory { get; set; }

public override BulkInsertOperation BulkInsert(string database = null, BulkInsertOptions options = null)
{
Expand Down
6 changes: 3 additions & 3 deletions Raven.Client.Lightweight/FileSystem/FilesStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ public int MaxNumberOfCachedRequests
maxNumberOfCachedRequests = value;
if (jsonRequestFactory != null)
jsonRequestFactory.Dispose();
jsonRequestFactory = new HttpJsonRequestFactory(maxNumberOfCachedRequests, HttpMessageHandler);
jsonRequestFactory = new HttpJsonRequestFactory(maxNumberOfCachedRequests, HttpMessageHandlerFactory);
}
}

public HttpMessageHandler HttpMessageHandler { get; set; }
public Func<HttpMessageHandler> HttpMessageHandlerFactory { get; set; }

private string url;

Expand Down Expand Up @@ -205,7 +205,7 @@ public IFilesStore Initialize(bool ensureFileSystemExists = true, bool failIfCan
if (initialized)
return this;

jsonRequestFactory = new HttpJsonRequestFactory(MaxNumberOfCachedRequests, HttpMessageHandler);
jsonRequestFactory = new HttpJsonRequestFactory(MaxNumberOfCachedRequests, HttpMessageHandlerFactory);

try
{
Expand Down
6 changes: 4 additions & 2 deletions Raven.Database/Server/RavenDbServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;

using Raven.Abstractions.Replication;
Expand Down Expand Up @@ -106,11 +107,12 @@ public RavenDbServer Initialize(Action<RavenDBOptions> configure = null)
owinHttpServer = new OwinHttpServer(configuration, useHttpServer: UseEmbeddedHttpServer, configure: configure);
options = owinHttpServer.Options;
serverThingsForTests = new ServerThingsForTests(options);
documentStore.HttpMessageHandler = new OwinClientHandler(owinHttpServer.Invoke, options.SystemDatabase.Configuration.EnableResponseLoggingForEmbeddedDatabases);
Func<HttpMessageHandler> httpMessageHandlerFactory = ()=>new OwinClientHandler(owinHttpServer.Invoke, options.SystemDatabase.Configuration.EnableResponseLoggingForEmbeddedDatabases);
documentStore.HttpMessageHandlerFactory = httpMessageHandlerFactory;
documentStore.Url = string.IsNullOrWhiteSpace(Url) ? "http://localhost" : Url;
documentStore.Initialize();

filesStore.HttpMessageHandler = new OwinClientHandler(owinHttpServer.Invoke, options.SystemDatabase.Configuration.EnableResponseLoggingForEmbeddedDatabases);
filesStore.HttpMessageHandlerFactory = httpMessageHandlerFactory;
filesStore.Url = string.IsNullOrWhiteSpace(Url) ? "http://localhost" : Url;
filesStore.Initialize();

Expand Down
2 changes: 1 addition & 1 deletion Raven.Tests.Core/RavenCoreTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected virtual DocumentStore GetDocumentStore([CallerMemberName] string datab

var documentStore = new DocumentStore
{
HttpMessageHandler = Server.DocumentStore.HttpMessageHandler,
HttpMessageHandlerFactory = Server.DocumentStore.HttpMessageHandlerFactory,
Url = Server.SystemDatabase.ServerUrl,
DefaultDatabase = databaseName
};
Expand Down
44 changes: 44 additions & 0 deletions Raven.Tests.MailingList/CustomHttpMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Net.Http;
using Raven.Client.Document;
using Raven.Tests.Common;
using Xunit;

namespace Raven.Tests.MailingList
{
public class CustomHttpMessageHandler : RavenTest
{
private class Doc
{
public string Id { get; set; }
}

[Fact]
public void CanSetHandler()
{
Action<DocumentStore> setHandler = store =>
{
store.HttpMessageHandlerFactory = () => new WebRequestHandler
{
UnsafeAuthenticatedConnectionSharing = true,
ReadWriteTimeout = 5 * 60 * 1000
};
};

using (var store = NewRemoteDocumentStore(configureStore: setHandler))
{
using (var session = store.OpenSession())
{
session.Store(new Doc { Id = "ayende" });
session.SaveChanges();
}

using (var session = store.OpenSession())
{
var doc = session.Load<Doc>("ayende");
Assert.NotNull(doc);
}
}
}
}
}
2 changes: 2 additions & 0 deletions Raven.Tests.MailingList/Raven.Tests.MailingList.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Reactive.Core, Version=2.2.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Rx-Core.2.2.5\lib\net45\System.Reactive.Core.dll</HintPath>
Expand Down Expand Up @@ -183,6 +184,7 @@
<Compile Include="ConcurrencyTests2.cs" />
<Compile Include="ConcurrentDictionaryInheritence.cs" />
<Compile Include="CustomAnalyzerStartsWithFailure.cs" />
<Compile Include="CustomHttpMessageHandler.cs" />
<Compile Include="CustomIdInIndexCreationTask.cs" />
<Compile Include="Daniel.cs" />
<Compile Include="DanielPilon.cs" />
Expand Down

0 comments on commit 740ad10

Please sign in to comment.