View | Details | Raw Unified | Return to bug 16212
Collapse All | Expand All

(-)a/Koha/REST/V1.pm (+26 lines)
Lines 43-48 sub startup { Link Here
43
    # Force charset=utf8 in Content-Type header for JSON responses
43
    # Force charset=utf8 in Content-Type header for JSON responses
44
    $self->types->type(json => 'application/json; charset=utf8');
44
    $self->types->type(json => 'application/json; charset=utf8');
45
45
46
    $self->minifySwagger();
47
46
    my $secret_passphrase = C4::Context->config('api_secret_passphrase');
48
    my $secret_passphrase = C4::Context->config('api_secret_passphrase');
47
    if ($secret_passphrase) {
49
    if ($secret_passphrase) {
48
        $self->secrets([$secret_passphrase]);
50
        $self->secrets([$secret_passphrase]);
Lines 54-57 sub startup { Link Here
54
    });
56
    });
55
}
57
}
56
58
59
sub minifySwagger {
60
    my ($self, $swaggerPath) = @_;
61
62
    $swaggerPath = C4::Context->config("intranetdir").'/api/v1/' unless $swaggerPath;
63
    my $pathToMinifier = $swaggerPath.'minifySwagger.pl';
64
    my $pathToSwaggerJson = $swaggerPath.'swagger.json';
65
    my $pathToSwaggerMinJson = $swaggerPath.'swagger.min.json';
66
67
    if (_isMinificationRequired($pathToSwaggerJson, $pathToSwaggerMinJson)) {
68
        my $output = `perl $pathToMinifier -s $pathToSwaggerJson -d $pathToSwaggerMinJson`;
69
        if ($output) {
70
            die $output;
71
        }
72
    }
73
}
74
75
sub _isMinificationRequired {
76
    my ($pathToSwaggerJson, $pathToSwaggerMinJson) = @_;
77
78
    return 1 unless -e $pathToSwaggerJson; # swagger.json exists?
79
    return 1 unless -e $pathToSwaggerMinJson; # swagger.min.json exists?
80
    return 1 if -C $pathToSwaggerJson < -C $pathToSwaggerMinJson; # compare modification time
81
}
82
57
1;
83
1;
(-)a/t/api/v1/minifier.t (-1 / +119 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# Copyright (C) 2016 KohaSuomi
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
use File::Copy qw/copy/;
23
use File::Temp;
24
use File::Path qw/make_path rmtree/;
25
26
use Test::More tests => 9;
27
28
use C4::Context;
29
use Koha::REST::V1;
30
31
my $oldSwagger_title = "Swagger Sample App";
32
my $newSwagger_title = "Koha REST API";
33
34
# Construct an example swagger.json
35
my $swaggerJsonTest =
36
qq ({
37
  "swagger": "2.0",
38
  "info": {
39
    "title": "$oldSwagger_title",
40
    "version": "1"
41
  },
42
  "paths": {},
43
  "definitions": {},
44
  "parameters": {}
45
});
46
47
my $swaggerJsonTestModified =
48
qq ({
49
  "swagger": "2.0",
50
  "info": {
51
    "title": "$newSwagger_title",
52
    "version": "1"
53
  },
54
  "paths": {},
55
  "definitions": {},
56
  "parameters": {}
57
});
58
59
# Path containing actual swagger specification files
60
my $real_swaggerPath = C4::Context->config("intranetdir").'/api/v1/';
61
62
# Create a tmp directory where we can modify swagger.json
63
my $swaggerPath = File::Temp->newdir()."/";
64
make_path($swaggerPath);
65
66
# Copy minifySwagger.pl to this location
67
copy($real_swaggerPath.'minifySwagger.pl', $swaggerPath);
68
69
my $pathToMinifier = $swaggerPath.'minifySwagger.pl';
70
my $pathToSwaggerJson = $swaggerPath.'swagger.json';
71
my $pathToSwaggerMinJson = $swaggerPath.'swagger.min.json';
72
73
is(-e $pathToSwaggerJson, undef, "swagger.json does not yet exist");
74
is(-e $pathToSwaggerMinJson, undef, "swagger.min.json does not yet exist");
75
76
my $exists = -e $pathToSwaggerJson;
77
# Create swagger.json test file
78
open my $fh, '>', $pathToSwaggerJson;
79
print $fh $swaggerJsonTest;
80
close $fh;
81
82
ok(-e $pathToSwaggerJson, "swagger.json created!");
83
84
Koha::REST::V1::minifySwagger(undef, $swaggerPath);
85
ok(-e $pathToSwaggerMinJson, "swagger.min.json created!");
86
my $lastmodified = -C $pathToSwaggerMinJson;
87
88
# Read swagger.min.json
89
require Swagger2;
90
my $swaggerMin = Swagger2->new($pathToSwaggerMinJson);
91
$swaggerMin = $swaggerMin->expand;
92
my $oldSwaggerTitle = $swaggerMin->{'api_spec'}->{'data'}->{'info'}->{'title'};
93
is($oldSwaggerTitle, $oldSwagger_title, "old title found");
94
95
# Modify swagger.json
96
sleep(1); # make sure modification time differs from swagger.min.json
97
open $fh, '>', $pathToSwaggerJson;
98
print $fh $swaggerJsonTestModified;
99
close $fh;
100
101
Koha::REST::V1::minifySwagger(undef, $swaggerPath);
102
103
ok($lastmodified > -C $pathToSwaggerMinJson,
104
   "swagger.min.json modified after editing swagger.json");
105
$lastmodified = -C $pathToSwaggerMinJson;
106
107
# Re-read swagger.min.json
108
$swaggerMin = Swagger2->new($pathToSwaggerMinJson);
109
$swaggerMin = $swaggerMin->expand;
110
my $newSwaggerTitle = $swaggerMin->{'api_spec'}->{'data'}->{'info'}->{'title'};
111
is($newSwaggerTitle, $newSwagger_title, "new title found"); # was minified
112
113
Koha::REST::V1::minifySwagger(undef, $swaggerPath);
114
ok($lastmodified == -C $pathToSwaggerMinJson,
115
   "swagger.min.json not modified because no modifications to swagger.json");
116
117
# Cleanup
118
rmtree($swaggerPath) if $swaggerPath ne $real_swaggerPath;
119
is(-e $swaggerPath, undef, "tmp files deleted");

Return to bug 16212