-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from ahwm/httpclient-update
Httpclient update
- Loading branch information
Showing
10 changed files
with
235 additions
and
385 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#if NETSTANDARD | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Reflection; | ||
|
||
namespace Akismet.Net | ||
{ | ||
public static class ServicesExtension | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="services"></param> | ||
/// <param name="key"></param> | ||
/// <param name="username"></param> | ||
/// <param name="test"></param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddAkismet(this IServiceCollection services, string key, string applicationName, string blogUrl) | ||
{ | ||
// https://stackoverflow.com/a/79111722/1892993 | ||
services.AddOptions<AkismetClientOptions>() | ||
.Configure(options => { | ||
options.Key = key; | ||
options.BlogUrl = blogUrl; | ||
}); | ||
services.AddHttpClient<AkismetClient>(client => | ||
{ | ||
client.BaseAddress = new Uri($"https://{key}.rest.akismet.com/1.1/"); | ||
client.DefaultRequestHeaders.Add("User-Agent", $"{applicationName} | Akismet.NET/{Assembly.GetExecutingAssembly().GetName().Version} (https://github.com/ahwm/Akismet.Net)"); | ||
}); | ||
|
||
return services; | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
using Akismet.Net; | ||
using Shouldly; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Akismet.Tests | ||
{ | ||
public class AkismetTests | ||
{ | ||
private readonly string ApiKey; | ||
private readonly string ApiKeyUrl; | ||
Check warning on line 11 in Akismet.Tests/AkismetTests.cs GitHub Actions / build
Check warning on line 11 in Akismet.Tests/AkismetTests.cs GitHub Actions / build
Check warning on line 11 in Akismet.Tests/AkismetTests.cs GitHub Actions / build
Check warning on line 11 in Akismet.Tests/AkismetTests.cs GitHub Actions / build
Check warning on line 11 in Akismet.Tests/AkismetTests.cs GitHub Actions / build
|
||
private readonly AkismetClient Client; | ||
#if NETCORE | ||
public AkismetTests(AkismetClient akismetClient) | ||
{ | ||
Client = akismetClient; | ||
} | ||
#else | ||
private readonly string ApiKey; | ||
|
||
public AkismetTests() | ||
{ | ||
|
@@ -23,12 +25,7 @@ public AkismetTests() | |
|
||
Client = new AkismetClient(ApiKey, new Uri(ApiKeyUrl), "Akismet Test Application"); | ||
} | ||
|
||
[Fact] | ||
public void VerifyKeyTest() | ||
{ | ||
Client.VerifyKey().ShouldBe(true); | ||
} | ||
#endif | ||
|
||
[Fact] | ||
public async Task VerifyKeyAsyncTest() | ||
|
@@ -38,29 +35,6 @@ public async Task VerifyKeyAsyncTest() | |
isValid.ShouldBe(true); | ||
} | ||
|
||
[Fact] | ||
public void CheckSpamComment() | ||
{ | ||
AkismetComment comment = new AkismetComment | ||
{ | ||
CommentAuthor = "viagra-test-123", | ||
CommentAuthorEmail = "[email protected]", | ||
CommentAuthorUrl = "http://www.spamwebsite.com", | ||
Referrer = "https://www.google.com", | ||
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36", | ||
CommentContent = "This is a test spam comment", | ||
CommentType = AkismentCommentType.ContactForm, // multiple defined values, or use new AkismetCommentType("new-comment-type") for a custom option | ||
Permalink = $"{ApiKeyUrl}/contact", | ||
IsTest = "true", | ||
BlogCharset = "UTF-8", | ||
BlogLanguage = "en-US", | ||
CommentDate = DateTime.UtcNow.ToString("s"), // ISO-8601 format | ||
CommentPostModified = DateTime.UtcNow.ToString("s") // ISO-8601 format | ||
}; | ||
var spamResult = Client.Check(comment); | ||
spamResult.SpamStatus.ShouldBe(SpamStatus.Spam); | ||
} | ||
|
||
[Fact] | ||
public async Task CheckSpamCommentAsync() | ||
{ | ||
|
@@ -84,30 +58,6 @@ public async Task CheckSpamCommentAsync() | |
spamResult.SpamStatus.ShouldBe(SpamStatus.Spam); | ||
} | ||
|
||
[Fact] | ||
public void CheckHamComment() | ||
{ | ||
AkismetComment comment = new AkismetComment | ||
{ | ||
CommentAuthor = "Test", | ||
CommentAuthorEmail = "[email protected]", | ||
CommentAuthorUrl = "http://www.spamwebsite.com", | ||
Referrer = "https://www.google.com", | ||
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36", | ||
CommentContent = "This is a test ham comment", | ||
CommentType = AkismentCommentType.ContactForm, | ||
Permalink = $"{ApiKeyUrl}/contact", | ||
IsTest = "true", | ||
BlogCharset = "UTF-8", | ||
BlogLanguage = "en-US", | ||
CommentDate = DateTime.UtcNow.ToString("s"), | ||
CommentPostModified = DateTime.UtcNow.ToString("s"), | ||
UserRole = "administrator" | ||
}; | ||
var spamResult = Client.Check(comment); | ||
spamResult.SpamStatus.ShouldBe(SpamStatus.Ham); | ||
} | ||
|
||
[Fact] | ||
public async Task CheckHamCommentAsync() | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#if NETCORE | ||
using Akismet.Net; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
|
||
namespace Akismet.Tests | ||
{ | ||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
string apiKey = Environment.GetEnvironmentVariable("AKISMET_API_KEY").Trim(); | ||
string blogUrl = Environment.GetEnvironmentVariable("AKISMET_API_KEY_URL").Trim(); | ||
services.AddAkismet(apiKey, "Akismet Test Application", blogUrl); | ||
} | ||
} | ||
} | ||
#endif |
Oops, something went wrong.