How to use with GitHubClient? #384

Closed
opened 2025-12-29 15:22:26 +01:00 by adam · 1 comment
Owner

Originally created by @gpetrou on GitHub (Nov 13, 2021).

I would like to use WireMock.net with Octokit.
If I use the following code and UseWireMock is false, I get the expected number of repositories.
If it is true and therefore it will use WireMock.net localhost URL, I get a NotFoundException.
Since this is my first time using WireMock.net, I suspect I might be doing a rookie mistake with the options.
Any idea what that might be?

using Octokit;
using WireMock.Server;
using WireMock.Settings;

const string AccessToken = "YOUR_GITHUB_TOKEN";
const string AccountName = "YOUR_GITHUB_USER_NAME";
const bool UseWireMock = true;

var wireMockServerSettings = new WireMockServerSettings()
{
    Urls = new[] { "https://localhost:15000/" },
    StartAdminInterface = true,
    ProxyAndRecordSettings = new ProxyAndRecordSettings
    {
        Url = "https://api.github.com/",
        SaveMapping = true,
        SaveMappingToFile = true,
        AllowAutoRedirect = true
    }
};

var wireMockServer = WireMockServer.Start(wireMockServerSettings);

GitHubClient gitHubClient;
if (UseWireMock)
{
    gitHubClient = new GitHubClient(new ProductHeaderValue("WireMockTest"), new Uri(wireMockServer.Urls[0]));
}
else
{
    gitHubClient = new GitHubClient(new ProductHeaderValue("WireMockTest"));
}

gitHubClient.Credentials = new Credentials(AccessToken);

var repositories = gitHubClient.Repository.GetAllForUser(AccountName).GetAwaiter().GetResult();

var repositoryCount = repositories.Count();

Console.WriteLine($"{repositoryCount}");
Originally created by @gpetrou on GitHub (Nov 13, 2021). I would like to use WireMock.net with [Octokit](https://github.com/octokit/octokit.net). If I use the following code and UseWireMock is false, I get the expected number of repositories. If it is true and therefore it will use WireMock.net localhost URL, I get a NotFoundException. Since this is my first time using WireMock.net, I suspect I might be doing a rookie mistake with the options. Any idea what that might be? ``` c# using Octokit; using WireMock.Server; using WireMock.Settings; const string AccessToken = "YOUR_GITHUB_TOKEN"; const string AccountName = "YOUR_GITHUB_USER_NAME"; const bool UseWireMock = true; var wireMockServerSettings = new WireMockServerSettings() { Urls = new[] { "https://localhost:15000/" }, StartAdminInterface = true, ProxyAndRecordSettings = new ProxyAndRecordSettings { Url = "https://api.github.com/", SaveMapping = true, SaveMappingToFile = true, AllowAutoRedirect = true } }; var wireMockServer = WireMockServer.Start(wireMockServerSettings); GitHubClient gitHubClient; if (UseWireMock) { gitHubClient = new GitHubClient(new ProductHeaderValue("WireMockTest"), new Uri(wireMockServer.Urls[0])); } else { gitHubClient = new GitHubClient(new ProductHeaderValue("WireMockTest")); } gitHubClient.Credentials = new Credentials(AccessToken); var repositories = gitHubClient.Repository.GetAllForUser(AccountName).GetAwaiter().GetResult(); var repositoryCount = repositories.Count(); Console.WriteLine($"{repositoryCount}"); ```
adam added the question label 2025-12-29 15:22:26 +01:00
adam closed this issue 2025-12-29 15:22:26 +01:00
Author
Owner

@gpetrou commented on GitHub (Nov 15, 2021):

Looks like this was an issue with the GitHubClient constructor that I was using. If I use the following instead it works:

var connection = new Connection(new ProductHeaderValue("WireMockTest"), new Uri(wireMockServer.Urls[0]));
gitHubClient = new GitHubClient(connection);
@gpetrou commented on GitHub (Nov 15, 2021): Looks like this was an issue with the GitHubClient constructor that I was using. If I use the following instead it works: ``` var connection = new Connection(new ProductHeaderValue("WireMockTest"), new Uri(wireMockServer.Urls[0])); gitHubClient = new GitHubClient(connection); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#384