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