Skip to content

Commit 13efb0c

Browse files
authored
[fix] Addresses issues when using client.content() and dealing with mismatched encoding
Fix broken encoding (fixes #920)
2 parents 22677ab + c9aa273 commit 13efb0c

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

lib/octokit/connection.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def request(method, path, data, options = {})
154154
end
155155

156156
@last_response = response = agent.call(method, Addressable::URI.parse(path.to_s).normalize.to_s, data, options)
157-
response.data
157+
response_data_correctly_encoded(response)
158158
rescue Octokit::Error => e
159159
@last_response = nil
160160
raise e
@@ -206,5 +206,13 @@ def parse_query_and_convenience_headers(options)
206206

207207
opts
208208
end
209+
210+
def response_data_correctly_encoded(response)
211+
content_type = response.headers.fetch('content-type', '')
212+
return response.data unless content_type.include?('charset') && response.data.is_a?(String)
213+
214+
reported_encoding = content_type.match(/charset=([^ ]+)/)[1]
215+
response.data.force_encoding(reported_encoding)
216+
end
209217
end
210218
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"http_interactions":[{"request":{"method":"get","uri":"https://api.github.com/repos/octokit/octokit.rb/contents/spec/fixtures/utf8.en.js","body":{"encoding":"US-ASCII","base64_string":""},"headers":{"Accept":["application/vnd.github.V3.raw"],"User-Agent":["Octokit Ruby Gem 4.14.0"],"Content-Type":["application/json"],"Authorization":["token <<ACCESS_TOKEN>>"],"Accept-Encoding":["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"]}},"response":{"status":{"code":200,"message":"OK"},"headers":{"Date":["Sun, 28 Apr 2019 22:51:27 GMT"],"Content-Type":["application/vnd.github.V3.raw; charset=utf-8"],"Content-Length":["67"],"Server":["GitHub.com"],"Status":["200 OK"],"X-Ratelimit-Limit":["5000"],"X-Ratelimit-Remaining":["4978"],"X-Ratelimit-Reset":["1556493266"],"Cache-Control":["private, max-age=60, s-maxage=60"],"Vary":["Accept, Authorization, Cookie, X-GitHub-OTP","Accept-Encoding"],"Etag":["\"a03fe032178ccc868a9c36935d55199088523cec\""],"Last-Modified":["Sun, 28 Apr 2019 22:42:09 GMT"],"X-Oauth-Scopes":["repo, user"],"X-Accepted-Oauth-Scopes":[""],"X-Github-Media-Type":["github.v3; param=V3.raw"],"Access-Control-Expose-Headers":["ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type"],"Access-Control-Allow-Origin":["*"],"Strict-Transport-Security":["max-age=31536000; includeSubdomains; preload"],"X-Frame-Options":["deny"],"X-Content-Type-Options":["nosniff"],"X-Xss-Protection":["1; mode=block"],"Referrer-Policy":["origin-when-cross-origin, strict-origin-when-cross-origin"],"Content-Security-Policy":["default-src 'none'"],"X-Github-Request-Id":["D9A1:55E3:8DF93A:AC7A70:5CC62E6E"]},"body":{"encoding":"ASCII-8BIT","base64_string":"Ly8gQGZsb3cKZXhwb3J0IGRlZmF1bHQgewogIGtleTogIlNvbWV0aGluZ+KA\npiB3aXRoIHV0ZjggY2hhcnMiLAp9Cg==\n"},"http_version":null},"recorded_at":"Sun, 28 Apr 2019 22:51:27 GMT"}],"recorded_with":"VCR 4.0.0"}

spec/fixtures/utf8.en.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @flow
2+
export default {
3+
key: "Something… with utf8 chars",
4+
}

spec/octokit/client/contents_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
expect(contents.type).to eq('file')
2626
assert_requested :get, github_url('/repos/octokit/octokit.rb/contents/lib/octokit.rb')
2727
end
28+
29+
it 'returns the contents of a file properly encoded as specified by the response headers' do
30+
contents = @client.contents('octokit/octokit.rb', path: 'spec/fixtures/utf8.en.js', accept: 'application/vnd.github.V3.raw')
31+
expect(contents).to eq(File.read('spec/fixtures/utf8.en.js'))
32+
expect(contents.encoding).to eq(Encoding::UTF_8)
33+
assert_requested :get, github_url('/repos/octokit/octokit.rb/contents/spec/fixtures/utf8.en.js')
34+
end
2835
end # .contents
2936

3037
describe '.archive_link', :vcr do

0 commit comments

Comments
 (0)