1<?php
2
3/**
4 * @group internet
5 */
6class fetch_statuscodes_external_test extends DokuWikiTest {
7
8    private $media = 'http://www.google.com/images/srpr/logo3w.png'; //used in media_get_from_url test too
9    private $width = 200;
10    private $height = 0;
11
12    function setUp() : void {
13
14        header('X-Test: check headers working');
15        $header_check = function_exists('xdebug_get_headers') ? xdebug_get_headers() : headers_list();
16        if(empty($header_check)) {
17            $this->markTestSkipped('headers not returned, perhaps your sapi does not return headers, try xdebug');
18        } else {
19            header_remove('X-Test');
20        }
21
22        parent::setUp();
23
24        global $conf;
25        $conf['fetchsize'] = 500 * 1024; //500kb
26        $conf['xsendfile'] = 0;
27
28        global $MIME, $EXT, $CACHE, $INPUT; // variables fetch creates in global scope -- should this be in fetch?
29    }
30
31    function getUri() {
32        $w = $this->width ? 'w='.$this->width.'&' : '';
33        $h = $this->height ? 'h='.$this->height.'&' : '';
34        return '/lib/exe/fetch.php?'.$w.$h.'{%token%}media='.rawurlencode($this->media);
35    }
36
37    function fetchResponse($token) {
38        $request = new TestRequest();
39        return $request->get(array(), str_replace('{%token%}', $token, $this->getUri()));
40    }
41
42    /**
43     *  modified image request with valid token
44     *  and not-modified image request with valid token
45     *
46     *  expect: header with mime-type
47     *  expect: content
48     *  expect: no error response
49     */
50    function test_valid_token() {
51        $valid_token_resize = 'tok='.media_get_token($this->media, $this->width, $this->height).'&';
52
53        $this->handlevalidresponse($valid_token_resize);
54
55        //original size
56        $this->width          = $this->height = 0;
57        $valid_token_original = 'tok='.media_get_token($this->media, $this->width, $this->height).'&';
58
59        $this->handlevalidresponse($valid_token_original);
60
61    }
62
63    /**
64     * Performs asserts for valid request
65     *
66     * @param $valid_token
67     */
68    private function handlevalidresponse($valid_token){
69        $response = $this->fetchResponse($valid_token);
70        $this->assertTrue((bool) $response->getHeader('Content-Type'));
71        $this->assertTrue((bool) ($response->getContent()));
72
73        $status_code = $response->getStatusCode();
74        $this->assertTrue(is_null($status_code) || (200 == $status_code));
75    }
76
77    /**
78     *  modified image request with invalid token
79     *  expect: 412 status code
80     */
81    function test_invalid_token() {
82        $invalid_tokens = array(
83            'invalid_token_wrongid' => media_get_token('junk', 200, 100),
84            'invalid_token_wrongh'  => media_get_token($this->media, 200, 10),
85            'invalid_token_wrongw'  => media_get_token($this->media, 20, 100),
86            'invalid_token_wrongwh' => media_get_token($this->media, 20, 10)
87        );
88        foreach($invalid_tokens as $invalid_token)
89            $this->assertEquals(412, $this->fetchResponse('tok='.$invalid_token.'&')->getStatusCode());
90
91    }
92
93    /**
94     *  modified image request with no token
95     *  and not modified image with no token
96     *  expect: 412 status code
97     */
98    function test_missing_token() {
99        $no_token = '';
100
101        $this->assertEquals(412, $this->fetchResponse($no_token)->getStatusCode());
102
103        $this->width = $this->height = 0;
104        $this->assertEquals(412, $this->fetchResponse($no_token)->getStatusCode());
105    }
106}
107//Setup VIM: ex: et ts=4 :
108