From 117c64a935aee6ac2677dfed04fa6722974f2399 Mon Sep 17 00:00:00 2001 From: Artem Vavilov Date: Fri, 9 Feb 2024 21:22:16 +0200 Subject: [PATCH] fix(build_project_file_translation): error when the `eTag` param is passed Issue: #74 --- lib/crowdin-api/api_resources/translations.rb | 3 +- spec/api_resources/translations_spec.rb | 43 +++++++++++++++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/lib/crowdin-api/api_resources/translations.rb b/lib/crowdin-api/api_resources/translations.rb index 0da678b..b53eea8 100644 --- a/lib/crowdin-api/api_resources/translations.rb +++ b/lib/crowdin-api/api_resources/translations.rb @@ -44,7 +44,8 @@ def build_project_file_translation(file_id = nil, query = {}, destination = nil, file_id || raise_parameter_is_required_error(:file_id) project_id || raise_project_id_is_required_error - headers = query[:eTag] ? { 'If-None-Match' => query[:eTag] } : {} + etag = query.delete(:eTag) + headers = etag ? { 'If-None-Match' => etag } : {} request = Web::Request.new( connection, diff --git a/spec/api_resources/translations_spec.rb b/spec/api_resources/translations_spec.rb index e517b6f..ae340f0 100644 --- a/spec/api_resources/translations_spec.rb +++ b/spec/api_resources/translations_spec.rb @@ -31,12 +31,49 @@ end describe '#build_project_file_translation' do + subject(:build_project_file_translation) do + @crowdin.build_project_file_translation(file_id, query, destination, project_id) + end + let(:file_id) { 1 } + let(:query) { { targetLanguageId: target_language_id } } + let(:destination) { nil } + + let(:target_language_id) { 'expected_language_id' } + + it 'builds project file translation', :default do + expected_response = 'expected_response' - it 'when request are valid', :default do stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/translations/builds/files/#{file_id}") - build_project_file_translation = @crowdin.build_project_file_translation(file_id, {}, nil, project_id) - expect(build_project_file_translation).to eq(200) + .with( + body: JSON.dump(query) + ) + .to_return( + body: JSON.dump(expected_response) + ) + + is_expected.to eq(expected_response) + end + + context 'when the `eTag` query param is passed' do + let(:query) { { targetLanguageId: 'expected_language_id', eTag: etag } } + + let(:etag) { 'expected_etag' } + + it 'builds project file translation for new changes after the passed `eTag`', :default do + expected_response = 'expected_response' + + stub_request(:post, "https://api.crowdin.com/#{target_api_url}/projects/#{project_id}/translations/builds/files/#{file_id}") + .with( + body: JSON.dump({ targetLanguageId: target_language_id }), + headers: { 'If-None-Match' => etag } + ) + .to_return( + body: JSON.dump(expected_response) + ) + + is_expected.to eq(expected_response) + end end end