Skip to content

Commit 02670c1

Browse files
authored
Merge pull request #15 from packbackbooks/service-connector-fix
DATAINF-97 Change how POST requests are made to fix double-encoded JSON
2 parents 4540bee + 744f0f4 commit 02670c1

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

src/Interfaces/ILtiServiceConnector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ interface ILtiServiceConnector
66
{
77
public function getAccessToken(array $scopes);
88

9-
public function makeServiceRequest(array $scopes, $method, $url, $body = null, $contentType = 'application/json', $accept = 'application/json');
9+
public function makeServiceRequest(array $scopes, string $method, string $url, string $body = null, $contentType = 'application/json', $accept = 'application/json');
1010
}

src/LtiServiceConnector.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function getAccessToken(array $scopes)
7575
return $tokenData['access_token'];
7676
}
7777

78-
public function makeServiceRequest(array $scopes, $method, $url, $body = null, $contentType = 'application/json', $accept = 'application/json')
78+
public function makeServiceRequest(array $scopes, string $method, string $url, string $body = null, $contentType = 'application/json', $accept = 'application/json')
7979
{
8080
$headers = [
8181
'Authorization' => 'Bearer '.$this->getAccessToken($scopes),
@@ -87,7 +87,7 @@ public function makeServiceRequest(array $scopes, $method, $url, $body = null, $
8787
$headers = array_merge($headers, ['Content-Type' => $contentType]);
8888
$response = $this->client->request($method, $url, [
8989
'headers' => $headers,
90-
'json' => $body,
90+
'body' => $body,
9191
]);
9292
break;
9393
default:
@@ -98,10 +98,13 @@ public function makeServiceRequest(array $scopes, $method, $url, $body = null, $
9898
}
9999

100100
$respHeaders = $response->getHeaders();
101+
array_walk($respHeaders, function (&$value) {
102+
$value = $value[0];
103+
});
101104
$respBody = $response->getBody();
102105

103106
return [
104-
'headers' => array_filter(explode("\r\n", $respHeaders)),
107+
'headers' => $respHeaders,
105108
'body' => json_decode($respBody, true),
106109
];
107110
}

tests/LtiServiceConnectorTest.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,31 @@
1313

1414
class LtiServiceConnectorTest extends TestCase
1515
{
16+
/**
17+
* @var Mockery\MockInterface
18+
*/
19+
private $registration;
20+
/**
21+
* @var Mockery\MockInterface
22+
*/
23+
private $cache;
24+
/**
25+
* @var Mockery\MockInterface
26+
*/
27+
private $client;
28+
/**
29+
* @var Mockery\MockInterface
30+
*/
31+
private $response;
32+
/**
33+
* @var string
34+
*/
35+
private $token;
36+
/**
37+
* @var LtiServiceConnector
38+
*/
39+
private $connector;
40+
1641
public function setUp(): void
1742
{
1843
$this->registration = Mockery::mock(ILtiRegistration::class);
@@ -69,30 +94,33 @@ public function testItMakesPostServiceRequest()
6994
$scopes = ['scopeKey'];
7095
$method = 'post';
7196
$url = 'https://example.com';
72-
$body = ['post' => 'body'];
97+
$body = json_encode(['post' => 'body']);
7398
$requestHeaders = [
7499
'Authorization' => 'Bearer '.$this->token,
75100
'Accept' => 'application/json',
76101
'Content-Type' => 'application/json',
77102
];
78103
$responseHeaders = [
79-
'Content-Type: application/json',
80-
'Accept: application/json',
104+
'Content-Type' => ['application/json'],
105+
'Server' => ['nginx'],
81106
];
82107
$responseBody = ['some' => 'response'];
83108
$expected = [
84-
'headers' => $responseHeaders,
109+
'headers' => [
110+
'Content-Type' => 'application/json',
111+
'Server' => 'nginx',
112+
],
85113
'body' => $responseBody,
86114
];
87115

88116
$this->mockCacheHasAccessToken();
89117
$this->client->shouldReceive('request')
90118
->with($method, $url, [
91119
'headers' => $requestHeaders,
92-
'json' => $body,
120+
'body' => $body,
93121
])->once()->andReturn($this->response);
94122
$this->response->shouldReceive('getHeaders')
95-
->once()->andReturn(implode("\r\n", $responseHeaders));
123+
->once()->andReturn($responseHeaders);
96124
$this->response->shouldReceive('getBody')
97125
->once()->andReturn(json_encode($responseBody));
98126

@@ -111,12 +139,15 @@ public function testItMakesDefaultServiceRequest()
111139
'Accept' => 'application/json',
112140
];
113141
$responseHeaders = [
114-
'Content-Type: application/json',
115-
'Accept: application/json',
142+
'Content-Type' => ['application/json'],
143+
'Server' => ['nginx'],
116144
];
117145
$responseBody = ['some' => 'response'];
118146
$expected = [
119-
'headers' => $responseHeaders,
147+
'headers' => [
148+
'Content-Type' => 'application/json',
149+
'Server' => 'nginx',
150+
],
120151
'body' => $responseBody,
121152
];
122153

@@ -126,7 +157,7 @@ public function testItMakesDefaultServiceRequest()
126157
'headers' => $requestHeaders,
127158
])->once()->andReturn($this->response);
128159
$this->response->shouldReceive('getHeaders')
129-
->once()->andReturn(implode("\r\n", $responseHeaders));
160+
->once()->andReturn($responseHeaders);
130161
$this->response->shouldReceive('getBody')
131162
->once()->andReturn(json_encode($responseBody));
132163

0 commit comments

Comments
 (0)