Skip to content

Commit

Permalink
Set Http client basic authentication header for typed instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
amitsingh5290 committed Oct 6, 2018
1 parent b95e39b commit bf10985
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using SolrNet.Impl;
using System.Collections.Generic;
using System.Linq;
using SolrNet.Microsoft.DependencyInjection;

namespace Microsoft.DependencyInjection.SolrNet.Tests
{
Expand Down Expand Up @@ -163,6 +164,26 @@ public void SetBasicAuthenticationHeader()
Assert.Equal(credentialsBase64, connection.HttpClient.DefaultRequestHeaders.Authorization.Parameter);
}

[Fact]
public void SetBasicAuthenticationHeaderForTypedInstance()
{
var sc = new ServiceCollection();

//my credentials
var credentials = System.Text.Encoding.ASCII.GetBytes("myUsername:myPassword");
//in base64
var credentialsBase64 = Convert.ToBase64String(credentials);
//use the options to set the Authorization header.
sc.AddSolrNet<string>("http://localhost:8983/solr", options => { options.HttpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentialsBase64); });

//test
var provider = sc.BuildServiceProvider();
var injectionConnection = provider.GetRequiredService<ISolrInjectedConnection<string>>() as BasicInjectionConnection<string>;
var connection = injectionConnection.Connection as AutoSolrConnection;
Assert.NotNull(connection.HttpClient.DefaultRequestHeaders.Authorization);
Assert.Equal(credentialsBase64, connection.HttpClient.DefaultRequestHeaders.Authorization.Parameter);
}

public class Entity { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ public static IServiceCollection AddSolrNet<TModel>(this IServiceCollection serv
}

services = BuildSolrNet(services, url, setupAction);
var connection = new BasicInjectionConnection<TModel>(new AutoSolrConnection(url));
//Set custom http client setting given by user at intiliation for specific solr core
var autoSolrConnection = new AutoSolrConnection(url);
var solrOption = new SolrNetOptions(autoSolrConnection.HttpClient);
setupAction(solrOption);
var connection = new BasicInjectionConnection<TModel>(autoSolrConnection);

services.AddTransient(typeof(ISolrInjectedConnection<TModel>), (service) => connection);
return services;
}
Expand Down

0 comments on commit bf10985

Please sign in to comment.