@@ -, +, @@ $ kshell k$ prove t/db_dependent/api/v1/auth.t containing what you set on the syspref --- Koha/REST/V1.pm | 17 +++++++++++++---- t/db_dependent/api/v1/auth.t | 21 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) --- a/Koha/REST/V1.pm +++ a/Koha/REST/V1.pm @@ -39,10 +39,19 @@ Overloaded Mojolicious->startup method. It is called at application startup. sub startup { my $self = shift; - # Remove /api/v1/app.pl/ from the path - $self->hook( before_dispatch => sub { - shift->req->url->base->path('/'); - }); + $self->hook( + before_dispatch => sub { + my $c = shift; + + # Remove /api/v1/app.pl/ from the path + $c->req->url->base->path('/'); + + # Handle CORS + $c->res->headers->header( 'Access-Control-Allow-Origin' => + C4::Context->preference('AccessControlAllowOrigin') ) + if C4::Context->preference('AccessControlAllowOrigin'); + } + ); # Force charset=utf8 in Content-Type header for JSON responses $self->types->type( json => 'application/json; charset=utf8' ); --- a/t/db_dependent/api/v1/auth.t +++ a/t/db_dependent/api/v1/auth.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 2; use Test::Mojo; use Test::Warn; @@ -107,6 +107,25 @@ subtest 'under() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'CORS support' => sub { + + plan tests => 6; + + t::lib::Mocks::mock_preference('AccessControlAllowOrigin',''); + $t->get_ok("/api/v1/patrons") + ->header_is( 'Access-control-allow-origin', undef, 'Header not returned' ); + # FIXME: newer Test::Mojo has header_exists_not + + t::lib::Mocks::mock_preference('AccessControlAllowOrigin',undef); + $t->get_ok("/api/v1/patrons") + ->header_is( 'Access-control-allow-origin', undef, 'Header not returned' ); + # FIXME: newer Test::Mojo has header_exists_not + + t::lib::Mocks::mock_preference('AccessControlAllowOrigin','*'); + $t->get_ok("/api/v1/patrons") + ->header_is( 'Access-control-allow-origin', '*', 'Header set' ); +}; + sub create_user_and_session { my $user = $builder->build( { --