xref: /dokuwiki/_test/tests/lib/exe/fetch_statuscodes_external.test.php (revision d572baf83c5a6d61a7f97aa4d115eba76c5f106c)
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() {
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($hash=null) {
32        $w = $this->width ? 'w='.$this->width.'&' : '';
33        $h = $this->height ? 'h='.$this->height.'&' : '';
34        if($hash === null) {
35            $hash = 'hash='.substr(md5(auth_cookiesalt().$this->media), 0, 6).'&';
36        }
37
38        return '/lib/exe/fetch.php?'.$hash.$w.$h.'{%token%}media='.rawurlencode($this->media);
39    }
40
41    function fetchResponse($token, $hash=null){
42        $request = new TestRequest();
43        return $request->get(array(),str_replace('{%token%}',$token,$this->getUri($hash)));
44    }
45
46    /**
47     * modified image request with invalid hash
48     * expect: 412 status code
49     */
50    function test_invalid_hash() {
51        $invalid_hash = 'hash='.substr(md5(auth_cookiesalt().'junk'), 0, 6).'&';
52        $token = 'tok='.media_get_token($this->media, $this->width, $this->height).'&';
53
54        $this->assertEquals(412,$this->fetchResponse($token, $invalid_hash)->getStatusCode());
55
56    }
57
58    /**
59     *  modified image request with valid token
60     *  expect: header with mime-type
61     *  expect: content
62     *  expect: no error response
63     */
64    function test_valid_token(){
65        $valid_token = 'tok='.media_get_token($this->media, $this->width, $this->height).'&';
66
67        $response = $this->fetchResponse($valid_token);
68        $this->assertTrue((bool)$response->getHeader('Content-Type'));
69        $this->assertTrue((bool)($response->getContent()));
70
71        $status_code = $response->getStatusCode();
72        $this->assertTrue(is_null($status_code) || (200 == $status_code));
73    }
74
75    /**
76     *  modified image request with invalid token
77     *  expect: 412 status code
78     */
79    function test_invalid_token(){
80        $invalid_token = 'tok='.media_get_token('junk',200,100).'&';
81        $this->assertEquals(412,$this->fetchResponse($invalid_token)->getStatusCode());
82    }
83
84    /**
85     *  modified image request with no token
86     *  expect: 412 status code
87     */
88    function test_missing_token(){
89        $no_token = '';
90        $this->assertEquals(412,$this->fetchResponse($no_token)->getStatusCode());
91    }
92
93    /**
94     *  native image request which doesn't require a token
95     *  try: with a token & without a token
96     *  expect: (for both) header with mime-type, content matching source image filesize & no error response
97     */
98    function test_no_token_required(){
99        $this->width = $this->height = 0;   // no width & height, means image request at native dimensions
100        $any_token = 'tok='.media_get_token('junk',200,100).'&';
101        $no_token = '';
102        $file = media_get_from_URL($this->media,'png', -1);
103        $bytes = filesize($file);
104
105        foreach(array($any_token, $no_token) as $token) {
106            $response = $this->fetchResponse($token);
107            $this->assertTrue((bool)$response->getHeader('Content-Type'));
108            $this->assertEquals(strlen($response->getContent()), $bytes);
109
110            $status_code = $response->getStatusCode();
111            $this->assertTrue(is_null($status_code) || (200 == $status_code));
112        }
113    }
114
115}
116//Setup VIM: ex: et ts=4 :
117