forked from octokit/octokit.net
-
Notifications
You must be signed in to change notification settings - Fork 1
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 octokit#917 from alfhenrik/feature-webhookhelper
Add helper class for creating web hooks
- Loading branch information
Showing
15 changed files
with
395 additions
and
12 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
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,108 @@ | ||
using System.Collections.Generic; | ||
using Xunit; | ||
|
||
namespace Octokit.Tests.Models | ||
{ | ||
public class NewRepositoryWebHookTests | ||
{ | ||
public class TheCtor | ||
{ | ||
string ExpectedRepositoryWebHookConfigExceptionMessage = | ||
"Duplicate webhook config values found - these values: Url should not be passed in as part of the config values. Use the properties on the NewRepositoryWebHook class instead."; | ||
|
||
[Fact] | ||
public void UsesDefaultValuesForDefaultConfig() | ||
{ | ||
var create = new NewRepositoryWebHook("windowsazure", new Dictionary<string, string>(), "http://test.com/example"); | ||
Assert.Equal(create.Url, "http://test.com/example"); | ||
Assert.Equal(create.ContentType, WebHookContentType.Form); | ||
Assert.Empty(create.Secret); | ||
Assert.False(create.InsecureSsl); | ||
|
||
var request = create.ToRequest(); | ||
Assert.Equal(request.Config.Count, 4); | ||
|
||
Assert.True(request.Config.ContainsKey("url")); | ||
Assert.True(request.Config.ContainsKey("content_type")); | ||
Assert.True(request.Config.ContainsKey("secret")); | ||
Assert.True(request.Config.ContainsKey("insecure_ssl")); | ||
|
||
Assert.Equal(request.Config["url"], "http://test.com/example"); | ||
Assert.Equal(request.Config["content_type"], WebHookContentType.Form.ToParameter()); | ||
Assert.Equal(request.Config["secret"], ""); | ||
Assert.Equal(request.Config["insecure_ssl"], "False"); | ||
} | ||
|
||
[Fact] | ||
public void CombinesUserSpecifiedContentTypeWithConfig() | ||
{ | ||
var config = new Dictionary<string, string> | ||
{ | ||
{"hostname", "http://hostname.url"}, | ||
{"username", "username"}, | ||
{"password", "password"} | ||
}; | ||
|
||
var create = new NewRepositoryWebHook("windowsazure", config, "http://test.com/example") | ||
{ | ||
ContentType = WebHookContentType.Json, | ||
Secret = string.Empty, | ||
InsecureSsl = true | ||
}; | ||
|
||
Assert.Equal(create.Url, "http://test.com/example"); | ||
Assert.Equal(create.ContentType, WebHookContentType.Json); | ||
Assert.Empty(create.Secret); | ||
Assert.True(create.InsecureSsl); | ||
|
||
var request = create.ToRequest(); | ||
|
||
Assert.Equal(request.Config.Count, 7); | ||
|
||
Assert.True(request.Config.ContainsKey("url")); | ||
Assert.True(request.Config.ContainsKey("content_type")); | ||
Assert.True(request.Config.ContainsKey("secret")); | ||
Assert.True(request.Config.ContainsKey("insecure_ssl")); | ||
|
||
Assert.Equal(request.Config["url"], "http://test.com/example"); | ||
Assert.Equal(request.Config["content_type"], WebHookContentType.Json.ToParameter()); | ||
Assert.Equal(request.Config["secret"], ""); | ||
Assert.Equal(request.Config["insecure_ssl"], true.ToString()); | ||
|
||
Assert.True(request.Config.ContainsKey("hostname")); | ||
Assert.Equal(request.Config["hostname"], config["hostname"]); | ||
Assert.True(request.Config.ContainsKey("username")); | ||
Assert.Equal(request.Config["username"], config["username"]); | ||
Assert.True(request.Config.ContainsKey("password")); | ||
Assert.Equal(request.Config["password"], config["password"]); | ||
} | ||
|
||
[Fact] | ||
public void ShouldThrowRepositoryWebHookConfigExceptionWhenDuplicateKeysExists() | ||
{ | ||
var config = new Dictionary<string, string> | ||
{ | ||
{"url", "http://example.com/test"}, | ||
{"hostname", "http://hostname.url"}, | ||
{"username", "username"}, | ||
{"password", "password"} | ||
}; | ||
|
||
var create = new NewRepositoryWebHook("windowsazure", config, "http://test.com/example") | ||
{ | ||
ContentType = WebHookContentType.Json, | ||
Secret = string.Empty, | ||
InsecureSsl = true | ||
}; | ||
|
||
Assert.Equal(create.Url, "http://test.com/example"); | ||
Assert.Equal(create.ContentType, WebHookContentType.Json); | ||
Assert.Empty(create.Secret); | ||
Assert.True(create.InsecureSsl); | ||
|
||
var ex = Assert.Throws<RepositoryWebHookConfigException>(() => create.ToRequest()); | ||
Assert.Equal(ExpectedRepositoryWebHookConfigExceptionMessage, ex.Message); | ||
} | ||
} | ||
} | ||
} |
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,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Runtime.Serialization; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Octokit | ||
{ | ||
#if !NETFX_CORE | ||
[Serializable] | ||
#endif | ||
[SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors", | ||
Justification = "These exceptions are specific to the GitHub API and not general purpose exceptions")] | ||
public class RepositoryWebHookConfigException : Exception | ||
{ | ||
readonly string message; | ||
|
||
public RepositoryWebHookConfigException(IEnumerable<string> invalidConfig) | ||
{ | ||
var parameterList = string.Join(", ", invalidConfig.Select(ic => ic.FromRubyCase())); | ||
message = string.Format(CultureInfo.InvariantCulture, | ||
"Duplicate webhook config values found - these values: {0} should not be passed in as part of the config values. Use the properties on the NewRepositoryWebHook class instead.", | ||
parameterList); | ||
} | ||
|
||
public override string Message | ||
{ | ||
get { return message; } | ||
} | ||
|
||
#if !NETFX_CORE | ||
/// <summary> | ||
/// Constructs an instance of RepositoryWebHookConfigException | ||
/// </summary> | ||
/// <param name="info"> | ||
/// The <see cref="SerializationInfo"/> that holds the | ||
/// serialized object data about the exception being thrown. | ||
/// </param> | ||
/// <param name="context"> | ||
/// The <see cref="StreamingContext"/> that contains | ||
/// contextual information about the source or destination. | ||
/// </param> | ||
protected RepositoryWebHookConfigException(SerializationInfo info, StreamingContext context) | ||
: base(info, context) | ||
{ | ||
if (info == null) return; | ||
message = info.GetString("Message"); | ||
} | ||
|
||
public override void GetObjectData(SerializationInfo info, StreamingContext context) | ||
{ | ||
base.GetObjectData(info, context); | ||
info.AddValue("Message", Message); | ||
} | ||
#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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Octokit | ||
{ | ||
public class WebHookConfigComparer : IEqualityComparer<KeyValuePair<string, string>> | ||
{ | ||
public bool Equals(KeyValuePair<string, string> x, KeyValuePair<string, string> y) | ||
{ | ||
return x.Key == y.Key; | ||
} | ||
|
||
public int GetHashCode(KeyValuePair<string, string> obj) | ||
{ | ||
return obj.Key.GetHashCode(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.