Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ jobs:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
- name: Install dependencies
run: pnpm install
- name: Lint
run: pnpm run lint
- name: Run Tests
run: pnpm run test
2 changes: 1 addition & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Generator as ServiceGenerator } from '@platformatic/service'
import { readFile } from 'node:fs/promises'
import { basename, resolve, join } from 'node:path'
import { resolve, join } from 'node:path'

export class Generator extends ServiceGenerator {
constructor (opts = {}) {
Expand Down
16 changes: 3 additions & 13 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { readFile } from 'node:fs/promises'
import { basename } from 'node:path'

const HTTP_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'TRACE']

const capitalizeHeaders = header => header.replace(/(^|-)([a-z])/g, (_, dash, letter) => dash + letter.toUpperCase());
const capitalizeHeaders = header => header.replace(/(^|-)([a-z])/g, (_, dash, letter) => dash + letter.toUpperCase())

export async function plugin (server, opts) {
// We import this dynically to provide better error reporting in case
Expand All @@ -25,6 +22,7 @@ export async function plugin (server, opts) {

// We accept all content-types and parse them as buffer, so that PHP can
// handle them
server.removeAllContentTypeParsers()
server.addContentTypeParser(/^.*/, { parseAs: 'buffer' }, (request, body, done) => {
done(null, body)
})
Expand Down Expand Up @@ -100,16 +98,8 @@ export async function plugin (server, opts) {
}

// A full URL string is needed for PHP, but Node.js splits that across a bunch of places.
function urlForRequest(req) {
function urlForRequest (req) {
const proto = req.raw.protocol ?? 'http:'
const host = req.headers.host ?? 'localhost'
return new URL(req.url, `${proto}//${host}`)
}

// Currently header values must be arrays. Need to make it support single values too.
function fixHeaders (headers) {
return Object.fromEntries(
Object.entries(headers)
.map(([key, value]) => [key, [value]])
)
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"version": "0.7.0",
"description": "Integration of PHP with Wattpm",
"scripts": {
"lint": "eslint --cache",
"lint:fix": "eslint --cache --fix",
"test": "node --test",
"build": "node -e 'import {schema} from \"./lib/schema.js\"; console.log(JSON.stringify(schema, null, 2))' > schema.json && json2ts > config.d.ts < schema.json",
"dl-wordpress": "rm -rf workdpress && curl -O https://wordpress.org/latest.zip && unzip latest.zip ; rm latest.zip",
Expand Down
26 changes: 26 additions & 0 deletions test/fixtures/hello/post-json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
// Set the content type to JSON
header('Content-Type: application/json');

// Get the raw POST data
$json_input = file_get_contents('php://input');

// Parse the JSON data
$data = json_decode($json_input, true);

// Check if JSON parsing was successful
if (json_last_error() !== JSON_ERROR_NONE) {
// Handle JSON parsing error
http_response_code(400);
echo json_encode(['error' => 'Invalid JSON: ' . json_last_error_msg()]);
exit;
}

// Ensure $data is an array (in case of null or other types)
if (!is_array($data)) {
$data = [];
}

// Output the parsed data as JSON with pretty printing
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
?>
2 changes: 1 addition & 1 deletion test/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ test('should generate a stackable app', async t => {
}

{
const files = await readdir(join(testDir, 'public'))
const files = await readdir(join(testDir, 'public'))
deepStrictEqual(files.sort(), ['index.php'])
}

Expand Down
27 changes: 21 additions & 6 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { buildServer } from '@platformatic/service'
import { randomUUID } from 'node:crypto'
import { once } from 'node:events'
import { resolve, join } from 'node:path'
import { join } from 'node:path'
import { test } from 'node:test'
import { stackable } from '../lib/index.js'
import formAutoContet from 'form-auto-content'
Expand Down Expand Up @@ -44,7 +42,7 @@ test('post data', async t => {
url: '/post.php',
method: 'POST',
...formAutoContet({
'foo': 'bar'
foo: 'bar'
})
})

Expand All @@ -60,8 +58,8 @@ test('get all headers', async t => {

t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.json(), {
'HTTP_USER_AGENT': 'lightMyRequest',
'HTTP_HOST': 'localhost:80'
HTTP_USER_AGENT: 'lightMyRequest',
HTTP_HOST: 'localhost:80'
})
})

Expand Down Expand Up @@ -94,3 +92,20 @@ test('support rewriter', async t => {
t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.body, 'Hello World!')
})

test('post JSON', async t => {
const server = await startStackable(t)
const res = await server.inject({
url: '/post-json.php',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ foo: 'bar' })
})

t.assert.deepStrictEqual(res.statusCode, 200)
t.assert.deepStrictEqual(res.json(), {
foo: 'bar'
})
})