Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock GitHub API #500

Merged
merged 2 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ dependencies {

testImplementation("com.typesafe:config:1.4.2")

testImplementation("org.mock-server:mockserver-netty:5.14.0")
testImplementation("org.mock-server:mockserver-client-java:5.14.0")

functionalTestImplementation("org.mock-server:mockserver-netty:5.14.0")
functionalTestImplementation("org.mock-server:mockserver-client-java:5.14.0")

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/micronaut/build/utils/GithubApiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public final class GithubApiUtils {
private static final String GH_TOKEN_PUBLIC_REPOS_READONLY = "GH_TOKEN_PUBLIC_REPOS_READONLY";
private static final String GH_USERNAME = "GH_USERNAME";

public static final String GITHUB_API_BASE_URL_SYSTEM_PROPERTY = "github.api.base.url";
public static final String GITHUB_BASE_API_URL = "https://api.github.com";

private GithubApiUtils() {
}

Expand All @@ -44,7 +47,7 @@ static byte[] fetchTagsFromGitHub(Logger logger, String slug) {
}

private static byte[] fetchFromGithub(Logger logger, String slug, String what) {
String url = "https://api.github.com/repos/" + normalizeSlug(slug) + "/" + what;
String url = System.getProperty(GITHUB_API_BASE_URL_SYSTEM_PROPERTY, GITHUB_BASE_API_URL) + "/repos/" + normalizeSlug(slug) + "/" + what;
try {
return fetchFromGithub(logger, connectionForGithubUrl(logger, url));
} catch (IOException ex) {
Expand Down
46 changes: 43 additions & 3 deletions src/test/groovy/io/micronaut/build/utils/GithubApiUtilsSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,55 @@ package io.micronaut.build.utils

import org.gradle.api.GradleException
import org.gradle.api.logging.Logger
import org.mockserver.integration.ClientAndServer
import org.mockserver.model.MediaType
import spock.lang.Shared
import spock.lang.Specification
import spock.util.environment.RestoreSystemProperties
import static org.mockserver.model.HttpRequest.request
import static org.mockserver.model.HttpResponse.response

@RestoreSystemProperties
class GithubApiUtilsSpec extends Specification {

@Shared
private ClientAndServer mockServer

def setupSpec() {
mockServer = ClientAndServer.startClientAndServer()
['tags', 'releases'].each { what ->
mockServer.when(
request()
.withMethod("GET")
.withPath("/repos/micronaut-projects/micronaut-security/$what")
).respond(
response()
.withStatusCode(200)
.withContentType(MediaType.JSON_UTF_8)
.withBody(GithubApiUtilsSpec.getResourceAsStream("/io.micronaut.build.utils/releases.json").bytes)
)
mockServer.when(
request()
.withMethod("GET")
.withPath("/repos/micronaut-projects/nope/$what")
).respond(
response()
.withStatusCode(404)
.withBody("Not found")
)
}

System.setProperty(GithubApiUtils.GITHUB_API_BASE_URL_SYSTEM_PROPERTY, "http://localhost:${mockServer.localPort}")
}

def cleanupSpec() {
mockServer.stop()
}

void "it is possible to fetch tags"() {
when:
String tags = new String(GithubApiUtils.fetchTagsFromGitHub(Stub(Logger), "micronaut-projects/micronaut-security"), "UTF-8")

println tags
melix marked this conversation as resolved.
Show resolved Hide resolved
then:
noExceptionThrown()
tags.contains("v")
Expand All @@ -18,7 +59,7 @@ class GithubApiUtilsSpec extends Specification {
void "it is possible to fetch releases"() {
when:
String releases = new String(GithubApiUtils.fetchReleasesFromGitHub(Stub(Logger), "micronaut-projects/micronaut-security"), "UTF-8")

println releases
melix marked this conversation as resolved.
Show resolved Hide resolved
then:
noExceptionThrown()
releases.contains("3.")
Expand All @@ -37,5 +78,4 @@ class GithubApiUtilsSpec extends Specification {
assert message.startsWith("Failed to read from Github API. Response code: 404")
}
}

}
Loading