1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\AppFramework\Middleware;
use OC\AppFramework\Middleware\CompressionMiddleware;
use OC\AppFramework\OCS\V1Response;
use OC\AppFramework\OCS\V2Response;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
class CompressionMiddlewareTest extends \Test\TestCase {
/** @var IRequest */
private $request;
/** @var Controller */
private $controller;
/** @var CompressionMiddleware */
private $middleWare;
protected function setUp(): void {
parent::setUp();
$this->request = $this->createMock(IRequest::class);
$this->middleWare = new CompressionMiddleware(
$this->request
);
$this->controller = $this->createMock(Controller::class);
}
public function testGzipOCSV1() {
$this->request->method('getHeader')
->with('Accept-Encoding')
->willReturn('gzip');
$response = $this->createMock(V1Response::class);
$response->expects($this->once())
->method('addHeader')
->with('Content-Encoding', 'gzip');
$response->method('getStatus')
->willReturn(Http::STATUS_OK);
$this->middleWare->beforeController($this->controller, 'myMethod');
$this->middleWare->afterController($this->controller, 'myMethod', $response);
$output = 'myoutput';
$result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
$this->assertSame($output, gzdecode($result));
}
public function testGzipOCSV2() {
$this->request->method('getHeader')
->with('Accept-Encoding')
->willReturn('gzip');
$response = $this->createMock(V2Response::class);
$response->expects($this->once())
->method('addHeader')
->with('Content-Encoding', 'gzip');
$response->method('getStatus')
->willReturn(Http::STATUS_OK);
$this->middleWare->beforeController($this->controller, 'myMethod');
$this->middleWare->afterController($this->controller, 'myMethod', $response);
$output = 'myoutput';
$result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
$this->assertSame($output, gzdecode($result));
}
public function testGzipJSONResponse() {
$this->request->method('getHeader')
->with('Accept-Encoding')
->willReturn('gzip');
$response = $this->createMock(JSONResponse::class);
$response->expects($this->once())
->method('addHeader')
->with('Content-Encoding', 'gzip');
$response->method('getStatus')
->willReturn(Http::STATUS_OK);
$this->middleWare->beforeController($this->controller, 'myMethod');
$this->middleWare->afterController($this->controller, 'myMethod', $response);
$output = 'myoutput';
$result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
$this->assertSame($output, gzdecode($result));
}
public function testNoGzipDataResponse() {
$this->request->method('getHeader')
->with('Accept-Encoding')
->willReturn('gzip');
$response = $this->createMock(DataResponse::class);
$response->expects($this->never())
->method('addHeader');
$response->method('getStatus')
->willReturn(Http::STATUS_OK);
$this->middleWare->beforeController($this->controller, 'myMethod');
$this->middleWare->afterController($this->controller, 'myMethod', $response);
$output = 'myoutput';
$result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
$this->assertSame($output, $result);
}
public function testNoGzipNo200() {
$this->request->method('getHeader')
->with('Accept-Encoding')
->willReturn('gzip');
$response = $this->createMock(JSONResponse::class);
$response->expects($this->never())
->method('addHeader');
$response->method('getStatus')
->willReturn(Http::STATUS_NOT_FOUND);
$this->middleWare->beforeController($this->controller, 'myMethod');
$this->middleWare->afterController($this->controller, 'myMethod', $response);
$output = 'myoutput';
$result = $this->middleWare->beforeOutput($this->controller, 'myMethod', $output);
$this->assertSame($output, $result);
}
}
|