Ich versuche, einen Rails-Test zu schreiben (mit Capybara & Poltergeist) zu testen. Zip-Datei-Download-Funktionalität.Schreiben in ZIP-Datei aus Binärdaten
Ich habe die binären Daten einer ZIP-Datei von einer XHR-Anfrage zurückgegeben und ich hoffe, diese Daten lokal in eine ZIP-Datei schreiben und von dort weitere Tests durchführen.
Die folgende Methode emuliert einen Klick auf eine Schaltfläche, die bei In-App, eine Zip-Datei aller Dateien zurückgibt, die ausgewählt wurden:
# Perform XHR
def download_file(link)
page.execute_script("window.downloadFile = function(){ var url = window.location.protocol + '//' + window.location.host + '#{link}'; return getFile(url); }")
page.execute_script("window.getFile = function(url){ var xhr = new XMLHttpRequest(); xhr.open('GET', url, false); xhr.responseType = 'blob'; xhr.send(); return xhr.response; }")
begin
file = page.evaluate_script('downloadFile()')
rescue
raise "Error during XHR. Is url valid?"
end
file
end
ich die Antwort zu schreiben, ich versuche hier in Datei:
file = download_file(url)
file_path = "#{Rails.root}/tmp/files/download.zip"
File.open(file_path, 'wb'){ |f| f.write file }
Beim Versuch, die resultierende Datei ich die folgende Antwort gegeben bin mit unzip tmp/files/download.zip
zu entpacken:
Archive: tmp/files/download.zip
caution: zipfile comment truncated
error [tmp/files/download.zip]: missing 3182550208 bytes in zipfile
(attempting to process anyway)
error [tmp/files/download.zip]: start of central directory not found;
zipfile corrupt.
(please check that you have transferred or created the zipfile in the
appropriate BINARY mode and that you have compiled UnZip properly)
I habe versucht, den MIME-Typ auf text/plain
, application/zip
usw. zu überschreiben, aber ohne Erfolg.
Irgendwelche Vorschläge?