Skip to content

Commit

Permalink
feat(render): specify Kroki instance through KROKI_ENDPOINT environ…
Browse files Browse the repository at this point in the history
…ment variable
  • Loading branch information
bauglir committed Apr 4, 2020
1 parent bfe6915 commit ff2e560
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Kroki.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,16 @@ RenderError(::Diagram, exception::Exception) = exception
"""
Renders a [`Diagram`](@ref) through a Kroki service to the specified output
format.
A `KROKI_ENDPOINT` environment variable can be set, specifying the URI of a
specific instance of Kroki to use (e.g. when using a [privately hosted
instance](https://docs.kroki.io/kroki/setup/install/)). By default the
[publicly hosted service](https://kroki.io) is used.
"""
render(diagram::Diagram{T}, output_format::AbstractString) where T <: Val = try
getfield(
request("GET", join([
"https://kroki.io",
get(ENV, "KROKI_ENDPOINT", "https://kroki.io"),
lowercase("$(T.parameters[1])"),
output_format,
UriSafeBase64Payload(diagram)
Expand Down
24 changes: 24 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,30 @@ end
InvalidOutputFormatError
)
end

@testset "`KROKI_ENDPOINT` environment variable" begin
# The instance of the Kroki service that is used for rendering can be
# controlled through the `KROKI_ENDPOINT` environment variable. The most
# straight-forward way for testing this is by pointing to an invalid
# endpoint and testing for a corresponding connection error
expected_diagram_type = :plantuml
expected_service_host = "https://localhost"

withenv("KROKI_ENDPOINT" => expected_service_host) do
try
render(Diagram(expected_diagram_type, "A -> B: C"), "svg")
catch exception
buffer = IOBuffer()
showerror(buffer, exception)
rendered_buffer = String(take!(buffer))

@test occursin("ECONNREFUSED", rendered_buffer)
@test occursin(
"$(expected_service_host)/$(expected_diagram_type)", rendered_buffer
)
end
end
end
end
end

Expand Down

0 comments on commit ff2e560

Please sign in to comment.