Skip to content

Commit

Permalink
feat: gzip payloads (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox authored Jul 10, 2023
1 parent 7199533 commit a296ba5
Show file tree
Hide file tree
Showing 16 changed files with 634 additions and 292 deletions.
1 change: 1 addition & 0 deletions .ameba.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Metrics/CyclomaticComplexity:
- src/coverage_reporter/parsers/lcov_parser.cr
- src/coverage_reporter/parsers/gcov_parser.cr
- src/coverage_reporter/parsers/coveragepy_parser.cr
- src/coverage_reporter/cli/cmd.cr

Lint/PercentArrays:
Enabled: true
Expand Down
12 changes: 0 additions & 12 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ shards:
git: https://github.com/crystal-ameba/ameba.git
version: 1.4.2

crest:
git: https://github.com/mamantoha/crest.git
version: 1.3.8

crystal-kcov:
git: https://github.com/vici37/crystal-kcov.git
version: 0.2.3+git.commit.7e49fe22d7d47040c9de77eb77a6daa76ce0655d
Expand All @@ -16,14 +12,6 @@ shards:
git: https://github.com/crystal-lang/crystal-db.git
version: 0.11.0

http-client-digest_auth:
git: https://github.com/mamantoha/http-client-digest_auth.git
version: 0.6.0

http_proxy:
git: https://github.com/mamantoha/http_proxy.git
version: 0.9.0

spectator:
git: https://gitlab.com/arctic-fox/spectator.git
version: 0.11.6
Expand Down
2 changes: 0 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ authors:
- Nick Merwin <[email protected]>

dependencies:
crest:
github: mamantoha/crest
sqlite3:
github: crystal-lang/crystal-sqlite3

Expand Down
120 changes: 96 additions & 24 deletions spec/coverage_reporter/api/jobs_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "../../spec_helper"
require "http"

Spectator.describe CoverageReporter::Api::Jobs do
subject { described_class.new(config, parallel, source_files, git_info) }
Expand All @@ -25,6 +26,48 @@ Spectator.describe CoverageReporter::Api::Jobs do
end

let(endpoint) { "#{CoverageReporter::Config::DEFAULT_ENDPOINT}/api/#{CoverageReporter::Api::Jobs::API_VERSION}/jobs" }
let(boundary) { CoverageReporter::Api::Jobs::BOUNDARY }
let(headers) do
{
"Content-Type" => "multipart/form-data; boundary=#{boundary}",
"X-Coveralls-Reporter" => "coverage-reporter",
"X-Coveralls-Reporter-Version" => CoverageReporter::VERSION,
"X-Coveralls-Coverage-Formats" => "cobertura",
"X-Coveralls-Source" => "cli",
}
end
let(request_body) do
data = config.to_h.merge({
:source_files => [
{
:name => "app/main.cr",
:coverage => [1, 2, nil],
},
{
:name => "app/helper.cr",
:coverage => [5, nil, 43],
},
],
:parallel => parallel,
:git => git_info,
:run_at => ENV["COVERALLS_RUN_AT"],
}).to_json.to_s

json_file = IO::Memory.new(
String.build do |io|
Compress::Gzip::Writer.open(io, &.print(data))
end
)

body = IO::Memory.new
HTTP::FormData.build(body, boundary) do |formdata|
metadata = HTTP::FormData::FileMetadata.new(filename: "json_file")
headers = HTTP::Headers{"Content-Type" => "application/gzip"}
formdata.file("json_file", json_file, metadata, headers)
end

body.to_s
end

before_each do
ENV["COVERALLS_RUN_AT"] = Time::Format::RFC_3339.format(Time.local)
Expand All @@ -37,32 +80,61 @@ Spectator.describe CoverageReporter::Api::Jobs do

it "calls the /jobs endpoint" do
WebMock.stub(:post, endpoint).with(
headers: {
"Content-Type" => "application/json",
"X-Coveralls-Reporter" => "coverage-reporter",
"X-Coveralls-Reporter-Version" => CoverageReporter::VERSION,
"X-Coveralls-Coverage-Formats" => "cobertura",
"X-Coveralls-Source" => "cli",
},
body: {
:json => config.to_h.merge({
:source_files => [
{
:name => "app/main.cr",
:coverage => [1, 2, nil],
},
{
:name => "app/helper.cr",
:coverage => [5, nil, 43],
},
],
:parallel => parallel,
:git => git_info,
:run_at => ENV["COVERALLS_RUN_AT"],
}).to_json.to_s,
}.to_json,
headers: headers,
body: request_body,
).to_return(status: 200, body: {:result => "ok"}.to_json)

subject.send_request
end

it "raises error when 500 is received" do
WebMock.stub(:post, endpoint).with(
headers: headers,
body: request_body,
).to_return(status: 500, body: {:result => "Internal Server Error"}.to_json)

expect { subject.send_request }.to raise_error(CoverageReporter::Api::InternalServerError)
end

it "raises error when 422 is received" do
WebMock.stub(:post, endpoint).with(
headers: headers,
body: request_body,
).to_return(status: 422, body: {:result => "Unprocessable Entity"}.to_json)

expect { subject.send_request }.to raise_error(CoverageReporter::Api::UnprocessableEntity)
end

it "redirects" do
redirect_url = "https://coveralls-redirect.io/api/v1/jobs"

WebMock.stub(:post, endpoint).with(
headers: headers,
body: request_body,
).to_return(status: 307, body: "Temporary redirect", headers: {"location" => redirect_url})

WebMock.stub(:post, redirect_url).with(
headers: headers,
body: request_body,
).to_return(status: 200, body: {:result => "ok"}.to_json)

expect { subject.send_request }.not_to raise_error
end

it "redirects relatively" do
redirect_path = "/api/v9/jobs"
redirect_url = "#{CoverageReporter::Config::DEFAULT_ENDPOINT}#{redirect_path}"

WebMock.stub(:post, endpoint).with(
headers: headers,
body: request_body,
).to_return(status: 302, body: "Found", headers: {"location" => redirect_path})

WebMock.stub(:post, redirect_url).with(
headers: headers,
body: request_body,
).to_return(status: 200, body: {:result => "ok"}.to_json)

expect { subject.send_request }.not_to raise_error
end
end
52 changes: 38 additions & 14 deletions spec/coverage_reporter/api/webhook_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,48 @@ Spectator.describe CoverageReporter::Api::Webhook do

after_each { WebMock.reset }

let(headers) do
{
"Content-Type" => "application/json",
"X-Coveralls-Reporter" => "coverage-reporter",
"X-Coveralls-Reporter-Version" => CoverageReporter::VERSION,
"X-Coveralls-Source" => "cli",
}
end

let(body) do
{
:repo_token => "token",
:carryforward => "flag1,flag2",
:payload => {
:build_num => nil,
:status => "done",
},
}.to_json
end

it "calls the /webhook endpoint" do
WebMock.stub(:post, endpoint).with(
headers: {
"Content-Type" => "application/json",
"X-Coveralls-Reporter" => "coverage-reporter",
"X-Coveralls-Reporter-Version" => CoverageReporter::VERSION,
"X-Coveralls-Source" => "cli",
},
body: {
:repo_token => "token",
:carryforward => "flag1,flag2",
:payload => {
:build_num => nil,
:status => "done",
},
}.to_json
headers: headers,
body: body
).to_return(status: 200, body: {"response" => "ok"}.to_json)

subject.send_request
end

it "follows the redirect" do
redirect_url = "https://coveralls-redirect.io/webhook"

WebMock.stub(:post, endpoint).with(
headers: headers,
body: body,
).to_return(status: 301, body: "Moved permanently", headers: {"location" => redirect_url})

WebMock.stub(:post, redirect_url).with(
headers: headers,
body: body
).to_return(status: 200, body: {"response" => "ok"}.to_json)

expect { subject.send_request }.not_to raise_error
end
end
Loading

0 comments on commit a296ba5

Please sign in to comment.