Completed
Push — master ( 7b80a9...c1406b )
by Jared
02:24
created

JCRequestTest::testPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
/**
4
 * Created by PhpStorm.
5
 * User: jaredchu
6
 * Date: 31/05/2017
7
 * Time: 10:14
8
 */
9
10
use JC\JCRequest;
11
12
class JCRequestTest extends PHPUnit_Framework_TestCase
13
{
14
    private $baseUrl = 'https://httpbin.org';
15
    private $params;
16
    private $headers;
17
18
    public function setUp()
19
    {
20
        $this->params = [
21
            'b' => 2,
22
            'c' => '3'
23
        ];
24
        $this->headers = [
25
            'User-Agent' => 'Jared Chu',
26
            'Accept' => 'application/json'
27
        ];
28
    }
29
30
    public function testGet()
31
    {
32
        $url = $this->baseUrl;
33
        $response = JCRequest::get($url);
34
        $this->assertEquals(200, $response->status());
35
36
        //test get with params
37
        $url = $this->baseUrl . '/get?a=1';
38
39
        $response = JCRequest::get($url, $this->params, $this->headers);
40
        $this->assertEquals(200, $response->status());
41
        $this->assertNotEmpty($response->body());
42
43
        $responseData = $response->json();
44
        $this->assertEquals('https://httpbin.org/get?a=1&b=2&c=3', $responseData->url);
45
        $this->assertEquals(1, $responseData->args->a);
46
        $this->assertEquals(2, $responseData->args->b);
47
        $this->assertEquals(3, $responseData->args->c);
48
49
        $this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'});
50
        $this->assertEquals('application/json', $responseData->headers->{'Accept'});
51
    }
52
53
    public function testPost()
54
    {
55
        $url = $this->baseUrl . '/post?a=1';
56
57
        $response = JCRequest::post($url, $this->params, $this->headers);
58
        $this->assertEquals(200, $response->status());
59
60
        $responseData = $response->json();
61
        $this->assertEquals('https://httpbin.org/post?a=1', $responseData->url);
62
        $this->assertEquals(1, $responseData->args->a);
63
        $this->assertEquals(2, $responseData->form->b);
64
        $this->assertEquals(3, $responseData->form->c);
65
66
        $this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'});
67
        $this->assertEquals('application/json', $responseData->headers->{'Accept'});
68
    }
69
70
    public function testPut()
71
    {
72
        $url = $this->baseUrl . '/put?a=1';
73
74
        $response = JCRequest::put($url, $this->params, $this->headers);
75
        $this->assertEquals(200, $response->status());
76
77
        $responseData = $response->json();
78
        $this->assertEquals('https://httpbin.org/put?a=1', $responseData->url);
79
        $this->assertEquals(1, $responseData->args->a);
80
        $this->assertEquals(2, $responseData->form->b);
81
        $this->assertEquals(3, $responseData->form->c);
82
83
        $this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'});
84
        $this->assertEquals('application/json', $responseData->headers->{'Accept'});
85
    }
86
87
    public function testPatch()
88
    {
89
        $url = $this->baseUrl . '/patch?a=1';
90
91
        $response = JCRequest::patch($url, $this->params, $this->headers);
92
        $this->assertEquals(200, $response->status());
93
94
        $responseData = $response->json();
95
        $this->assertEquals('https://httpbin.org/patch?a=1', $responseData->url);
96
        $this->assertEquals(1, $responseData->args->a);
97
        $this->assertEquals(2, $responseData->form->b);
98
        $this->assertEquals(3, $responseData->form->c);
99
100
        $this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'});
101
        $this->assertEquals('application/json', $responseData->headers->{'Accept'});
102
    }
103
104
    public function testDelete()
105
    {
106
        $url = $this->baseUrl . '/delete?a=1';
107
108
        $response = JCRequest::delete($url, $this->params, $this->headers);
109
        $this->assertEquals(200, $response->status());
110
111
        $responseData = $response->json();
112
        $this->assertEquals('https://httpbin.org/delete?a=1', $responseData->url);
113
        $this->assertEquals(1, $responseData->args->a);
114
        $this->assertEquals(2, $responseData->form->b);
115
        $this->assertEquals(3, $responseData->form->c);
116
117
        $this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'});
118
        $this->assertEquals('application/json', $responseData->headers->{'Accept'});
119
    }
120
121
    public function testHead()
122
    {
123
        $url = $this->baseUrl . '/headers';
124
125
        $response = JCRequest::head($url, $this->headers);
126
        $this->assertEquals(200, $response->status());
127
        $this->assertEmpty($response->body());
128
129
        $responseHeaders = $response->headers();
130
        $this->assertEquals('keep-alive', $responseHeaders['Connection'][0]);
131
        $this->assertEquals('application/json', $responseHeaders['Content-Type'][0]);
132
        $this->assertEquals('Flask', $responseHeaders['X-Powered-By'][0]);
133
        $this->assertEquals(144, $responseHeaders['Content-Length'][0]);
134
    }
135
136
    public function testJson()
137
    {
138
        $url = $this->baseUrl . '/post?a=1';
139
140
        $response = JCRequest::post($url, json_encode($this->params), $this->headers);
141
        $this->assertEquals(200, $response->status());
142
143
        $responseData = $response->json();
144
        $this->assertEquals('https://httpbin.org/post?a=1', $responseData->url);
145
        $this->assertEquals(1, $responseData->args->a);
146
        $this->assertEquals('{"b":2,"c":"3"}', $responseData->data);
147
148
        $this->assertEquals('Jared Chu', $responseData->headers->{'User-Agent'});
149
        $this->assertEquals('application/json', $responseData->headers->{'Accept'});
150
    }
151
}