Skip to content
4 changes: 2 additions & 2 deletions lib/SyTest/Assertions.pm
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ sub _assert_deeply_eq
croak "Got a value for '$key' that was not expected at $outerkeystr for $name";
}
}
elsif( $wanttype eq JSON_BOOLEAN_CLASS ) {
ref $got eq JSON_BOOLEAN_CLASS and $got eq $want or
elsif( $wanttype eq JSON_BOOLEAN_CLASS or $wanttype eq "JSON::number" ) {
$got eq $want or
croak "Got ${\ pp $got }, expected ${\ pp $want } at $outerkeystr for $name";
}
else {
Expand Down
59 changes: 51 additions & 8 deletions tests/00expect_http_fail.pl
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,61 @@ sub check_http_code
);
}

=head2 expect_matrix_error

# todo: generalise this to other http errors/matrix errcodes
sub expect_m_not_found
http_request()->main::expect_matrix_error(
$http_code, $matrix_errcode,
)->then( sub {
my ( $error_body ) = @_;
});

A decorator for a Future which we expect to return an HTTP error with a Matrix
error code. Asserts that the HTTP error code and matrix error code were as
expected, and if so returns the JSON-decoded body of the error (so that it can
be checked for additional fields).

=cut

sub expect_matrix_error
{
my $f = shift;
my ( $f, $expected_http_code, $expected_errcode ) = @_;

return $f->then_with_f(
sub { # done
my ( undef, $response ) = @_;

log_if_fail "Response", $response;
Future->fail(
"Expected to receive an HTTP $expected_http_code failure but it succeeded"
);
},
http => sub { # catch http
my ( $f, undef, undef, $response ) = @_;

$f->main::expect_http_404()
->then( sub {
$response and $response->code == $expected_http_code and
return Future->done( $response );

# if the error code doesn't match, return the failure.
return $f;
},
)->then( sub {
my ( $response ) = @_;
my $body = decode_json( $response->content );
assert_eq( $body->{errcode}, "M_NOT_FOUND", 'responsecode' );
Future->done( 1 );

log_if_fail "Error response body", $body;

assert_eq( $body->{errcode}, $expected_errcode, 'errcode' );
Future->done( $body );
});
}
push @EXPORT, qw/expect_m_not_found/;
push @EXPORT, qw( expect_matrix_error );


sub expect_m_not_found
{
my $f = shift;
return expect_matrix_error(
$f, 404, 'M_NOT_FOUND',
);
}
push @EXPORT, qw( expect_m_not_found );
Loading