2017-07-15 13 views
1

Mojoliciousで画像添付ファイルがフォームから正しく送信されたかどうかを確認するためのテストを作成しようとしています。 私は<input type="file" name="image_upload" accept="image/*">を含むフォームを持っています。コントローラでは、私は$self->req->upload('image_upload')->headers->content_type =~ /image/かどうかをチェックしています。反対側のテストでは、投稿要求内に画像ファイルとしてform => {image_upl => { content => 'abc', filename => 'x.png' }}が送信されます。アプリケーションの実行は正常に動作しますが、テストコード内でチェックが失敗します。私は実際にそれらのヘッダがどのように設定されているのか分からないので、私はポスト要求の中に模擬画像を送ることができます。ここMojoliciousでファイルの添付ファイルの種類をチェックするテストに失敗しました

コードである:

#Router. 
$if_admin->post('/attachment/:page_id/add')->name('on_attachment_add') 
    ->to('attachment#on_add'); 

# Client side (Mojolicious template). 
<%= form_for 'on_attachment_add' => { page_id => $self->stash('id') } 
     => (method => 'POST') => (enctype => "multipart/form-data") 
     => begin %> 
    <input type="file" name="image_upload" accept="image/*"> 
% end 

# Controller (on_add) 
my $self = shift; 
my $image_file = $self->req->upload('image_upload'); 
if (!$image_file || $image_file->slurp eq "" || !$image_file->headers || 
    !$image_file->headers->content_type || 
    !($image_file->headers->content_type =~ /image/)) { 
    $self->render(
    template => 'validation/invalid_data', 
    status => 400 
); 
} 

# test. 
$t->post_ok('/attachment/test_page/add' => form => { 
    image_upload => { content => 'aaa', filename => 'x.png' }, 
    image_name => 'new_image.jpg' 
})->status_is(200); 
+0

巨大な条件文は読みにくいです。あまり保守できません。 – simbabque

+1

あなたは実際にそうです。私はMVPを手に入れようとしていたので、そこにすべての小切手を入れました。私はそれをより明確なものに変更しています – Bianca

答えて

2

ここhttp://mojolicious.org/perldoc/Mojo/UserAgent/Transactor#txを見た後、私のような形でハッシュリファレンスに'Content-Type' => 'image/png'を追加:

$t->post_ok('/attachment/test_page/add' => form => { image_upload => 
    { content => 'abc', filename => 'x.png', 'Content-Type' => 'image/png' }, 
    image_name => 'new_image.jpg' 
})->status_is(200); 

これは問題を解決しました。

関連する問題