Skip to content

Commit 7758ea9

Browse files
Copilotkobenguyent
andcommitted
Changes before error encountered
Co-authored-by: kobenguyent <[email protected]>
1 parent 5dbd842 commit 7758ea9

File tree

5 files changed

+173
-20
lines changed

5 files changed

+173
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"test:appium-other": "mocha test/helper/Appium_test.js --grep 'second'",
5656
"test:ios:appium-quick": "mocha test/helper/Appium_ios_test.js --grep 'quick'",
5757
"test:ios:appium-other": "mocha test/helper/Appium_ios_test.js --grep 'second'",
58-
"test-app:start": "php -S 127.0.0.1:8000 -t test/data/app test/data/app/index.php",
58+
"test-app:start": "cd test/data/spa && npm run start",
5959
"test-app:stop": "kill -9 $(lsof -t -i:8000)",
6060
"test:unit:webbapi:playwright": "mocha test/helper/Playwright_test.js",
6161
"test:unit:webbapi:puppeteer": "mocha test/helper/Puppeteer_test.js",

test/data/spa/dist/bundle.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27232,10 +27232,15 @@ var CodeceptApp = (() => {
2723227232
import_react.default.useEffect(() => {
2723327233
const urlParams = new URLSearchParams(location.search)
2723427234
if (urlParams.has('posted')) {
27235-
fetch('/api/post-data.php')
27236-
.then(response => response.json())
27237-
.then(data => setPostData(data))
27238-
.catch(() => setPostData({}))
27235+
try {
27236+
const storedData = localStorage.getItem('codeceptjs_post_data')
27237+
if (storedData) {
27238+
setPostData(JSON.parse(storedData))
27239+
localStorage.removeItem('codeceptjs_post_data')
27240+
}
27241+
} catch (error) {
27242+
setPostData({})
27243+
}
2723927244
} else {
2724027245
setPostData({})
2724127246
}
@@ -27256,7 +27261,7 @@ var CodeceptApp = (() => {
2725627261
/* @__PURE__ */ (0, import_jsx_runtime.jsx)('div', {
2725727262
id: 'area1',
2725827263
'qa-id': 'test',
27259-
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)('a', { href: '/form/file', 'qa-id': 'test', 'qa-link': 'test', children: ' Test Link ' }),
27264+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)('a', { href: '/form/file', 'qa-id': 'test', 'qa-link': 'test', children: [' ', 'Test Link', ' '] }),
2726027265
}),
2726127266
/* @__PURE__ */ (0, import_jsx_runtime.jsx)('div', {
2726227267
id: 'area2',
@@ -27324,13 +27329,27 @@ var CodeceptApp = (() => {
2732427329
}
2732527330
function FormFilePage() {
2732627331
const postData = usePostData()
27332+
const navigate = useNavigate()
27333+
const handleSubmit = e => {
27334+
e.preventDefault()
27335+
const formData = new FormData(e.target)
27336+
const data = {}
27337+
for (let [key, value] of formData.entries()) {
27338+
if (value instanceof File) {
27339+
data[key] = `${value.name} (${value.size} bytes, ${value.type})`
27340+
} else {
27341+
data[key] = value
27342+
}
27343+
}
27344+
localStorage.setItem('codeceptjs_post_data', JSON.stringify(data))
27345+
navigate('/?posted=1')
27346+
}
2732727347
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)('div', {
2732827348
children: [
2732927349
/* @__PURE__ */ (0, import_jsx_runtime.jsx)('h1', { children: 'File Upload' }),
2733027350
/* @__PURE__ */ (0, import_jsx_runtime.jsx)('div', { className: 'notice', 'qa-id': 'test' }),
2733127351
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)('form', {
27332-
method: 'POST',
27333-
action: '/',
27352+
onSubmit: handleSubmit,
2733427353
encType: 'multipart/form-data',
2733527354
children: [
2733627355
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)('p', {
@@ -27366,13 +27385,23 @@ var CodeceptApp = (() => {
2736627385
}
2736727386
function FormHiddenPage() {
2736827387
const postData = usePostData()
27388+
const navigate = useNavigate()
27389+
const handleSubmit = e => {
27390+
e.preventDefault()
27391+
const formData = new FormData(e.target)
27392+
const data = {}
27393+
for (let [key, value] of formData.entries()) {
27394+
data[key] = value
27395+
}
27396+
localStorage.setItem('codeceptjs_post_data', JSON.stringify(data))
27397+
navigate('/?posted=1')
27398+
}
2736927399
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)('div', {
2737027400
children: [
2737127401
/* @__PURE__ */ (0, import_jsx_runtime.jsx)('h1', { children: 'Hidden Form' }),
2737227402
/* @__PURE__ */ (0, import_jsx_runtime.jsx)('div', { className: 'notice', 'qa-id': 'test' }),
2737327403
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)('form', {
27374-
method: 'POST',
27375-
action: '/',
27404+
onSubmit: handleSubmit,
2737627405
children: [
2737727406
/* @__PURE__ */ (0, import_jsx_runtime.jsx)('input', { type: 'hidden', name: 'hidden_field', value: 'hidden_value' }),
2737827407
/* @__PURE__ */ (0, import_jsx_runtime.jsxs)('p', {

test/data/spa/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"build": "npm run build-js && npm run copy-assets",
1313
"build-js": "npx esbuild src/index.js --bundle --outfile=dist/bundle.js --jsx=automatic --format=iife --global-name=CodeceptApp --target=es2018 --loader:.js=jsx",
1414
"copy-assets": "cp src/index.html dist/ && cp -r src/assets/* dist/ 2>/dev/null || true",
15-
"start": "npm run build && cd dist && python3 -m http.server 8000",
16-
"dev": "npm run build && cd dist && python3 -m http.server 8000"
15+
"start": "npm run build && node server.js",
16+
"dev": "npm run build && node server.js"
1717
},
1818
"devDependencies": {
1919
"esbuild": "^0.19.0"

test/data/spa/server.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const http = require('http')
2+
const fs = require('fs')
3+
const path = require('path')
4+
const url = require('url')
5+
6+
const PORT = 8000
7+
const DIST_DIR = path.join(__dirname, 'dist')
8+
9+
// MIME types for different file extensions
10+
const mimeTypes = {
11+
'.html': 'text/html',
12+
'.js': 'application/javascript',
13+
'.css': 'text/css',
14+
'.png': 'image/png',
15+
'.jpg': 'image/jpeg',
16+
'.jpeg': 'image/jpeg',
17+
'.gif': 'image/gif',
18+
'.svg': 'image/svg+xml',
19+
'.ico': 'image/x-icon',
20+
}
21+
22+
const server = http.createServer((req, res) => {
23+
const parsedUrl = url.parse(req.url, true)
24+
const pathname = parsedUrl.pathname
25+
26+
// Enable CORS for all requests
27+
res.setHeader('Access-Control-Allow-Origin', '*')
28+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
29+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization')
30+
31+
// Handle OPTIONS requests (preflight)
32+
if (req.method === 'OPTIONS') {
33+
res.writeHead(200)
34+
res.end()
35+
return
36+
}
37+
38+
// Check if it's a static file request
39+
const ext = path.extname(pathname)
40+
if (ext && mimeTypes[ext]) {
41+
const filePath = path.join(DIST_DIR, pathname)
42+
43+
fs.readFile(filePath, (err, data) => {
44+
if (err) {
45+
res.writeHead(404, { 'Content-Type': 'text/plain' })
46+
res.end('File not found')
47+
return
48+
}
49+
50+
res.writeHead(200, { 'Content-Type': mimeTypes[ext] })
51+
res.end(data)
52+
})
53+
return
54+
}
55+
56+
// For all other requests (React routes), serve index.html
57+
const indexPath = path.join(DIST_DIR, 'index.html')
58+
59+
fs.readFile(indexPath, (err, data) => {
60+
if (err) {
61+
res.writeHead(500, { 'Content-Type': 'text/plain' })
62+
res.end('Error loading index.html')
63+
return
64+
}
65+
66+
res.writeHead(200, { 'Content-Type': 'text/html' })
67+
res.end(data)
68+
})
69+
})
70+
71+
server.listen(PORT, '127.0.0.1', () => {
72+
console.log(`React SPA server running at http://127.0.0.1:${PORT}/`)
73+
})
74+
75+
// Graceful shutdown
76+
process.on('SIGINT', () => {
77+
console.log('\nShutting down server...')
78+
server.close(() => {
79+
console.log('Server closed.')
80+
process.exit(0)
81+
})
82+
})

test/data/spa/src/index.js

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22
import { createRoot } from 'react-dom/client'
33
import { BrowserRouter as Router, Routes, Route, useLocation, useNavigate } from 'react-router-dom'
44

5-
// Hook to get URL parameters and POST data from session
5+
// Hook to get URL parameters and POST data from localStorage
66
function usePostData() {
77
const [postData, setPostData] = React.useState({})
88
const location = useLocation()
@@ -11,11 +11,17 @@ function usePostData() {
1111
// Check if we're coming from a POST
1212
const urlParams = new URLSearchParams(location.search)
1313
if (urlParams.has('posted')) {
14-
// Fetch POST data from PHP session
15-
fetch('/api/post-data.php')
16-
.then(response => response.json())
17-
.then(data => setPostData(data))
18-
.catch(() => setPostData({}))
14+
// Get POST data from localStorage
15+
try {
16+
const storedData = localStorage.getItem('codeceptjs_post_data')
17+
if (storedData) {
18+
setPostData(JSON.parse(storedData))
19+
// Clear the POST data after displaying
20+
localStorage.removeItem('codeceptjs_post_data')
21+
}
22+
} catch (error) {
23+
setPostData({})
24+
}
1925
} else {
2026
setPostData({})
2127
}
@@ -105,13 +111,34 @@ function InfoPage() {
105111
// Form file page component
106112
function FormFilePage() {
107113
const postData = usePostData()
114+
const navigate = useNavigate()
115+
116+
const handleSubmit = e => {
117+
e.preventDefault()
118+
const formData = new FormData(e.target)
119+
const data = {}
120+
121+
// Handle file uploads and regular fields
122+
for (let [key, value] of formData.entries()) {
123+
if (value instanceof File) {
124+
// For file uploads, store file info (name, size, type)
125+
data[key] = `${value.name} (${value.size} bytes, ${value.type})`
126+
} else {
127+
data[key] = value
128+
}
129+
}
130+
131+
// Store in localStorage and redirect
132+
localStorage.setItem('codeceptjs_post_data', JSON.stringify(data))
133+
navigate('/?posted=1')
134+
}
108135

109136
return (
110137
<div>
111138
<h1>File Upload</h1>
112139
<div className="notice" qa-id="test"></div>
113140

114-
<form method="POST" action="/" encType="multipart/form-data">
141+
<form onSubmit={handleSubmit} encType="multipart/form-data">
115142
<p>
116143
<label>Upload a file:</label>
117144
<br />
@@ -142,13 +169,28 @@ function FormFilePage() {
142169
// Form hidden page component
143170
function FormHiddenPage() {
144171
const postData = usePostData()
172+
const navigate = useNavigate()
173+
174+
const handleSubmit = e => {
175+
e.preventDefault()
176+
const formData = new FormData(e.target)
177+
const data = {}
178+
179+
for (let [key, value] of formData.entries()) {
180+
data[key] = value
181+
}
182+
183+
// Store in localStorage and redirect
184+
localStorage.setItem('codeceptjs_post_data', JSON.stringify(data))
185+
navigate('/?posted=1')
186+
}
145187

146188
return (
147189
<div>
148190
<h1>Hidden Form</h1>
149191
<div className="notice" qa-id="test"></div>
150192

151-
<form method="POST" action="/">
193+
<form onSubmit={handleSubmit}>
152194
<input type="hidden" name="hidden_field" value="hidden_value" />
153195
<p>
154196
<label>Visible field:</label>

0 commit comments

Comments
 (0)