From 8cf8321087ea32d259c3a08a261dd04e41f32427 Mon Sep 17 00:00:00 2001 From: ramki-bruno Date: Mon, 19 May 2025 16:03:15 +0530 Subject: [PATCH] Added test requests for cookies and related changes in testbench server In bruno-testbench(packages/bruno-tests) server - Added support for customising response status-code in `POST /api/echo/custom` API using `statusCode` field - Added `/api/echo/trace` API which returns all the request details as an JSON response - Added SSL support for `localhost` in dev env to test HTTPS requests --- .../bruno-tests/collection/cookies/folder.bru | 4 +++ .../cookies/test secure+https,http/folder.bru | 4 +++ .../test secure+https,http/set and verify.bru | 31 +++++++++++++++++++ .../verify the cookie with http.bru | 16 ++++++++++ .../cookies/test secure+local-ipv4+http.bru | 29 +++++++++++++++++ .../cookies/test secure+local-ipv4+https.bru | 29 +++++++++++++++++ .../cookies/test secure+localhost+http.bru | 29 +++++++++++++++++ .../cookies/test secure+localhost+https.bru | 29 +++++++++++++++++ .../cookies/test secure+non-local+http.bru | 31 +++++++++++++++++++ .../cookies/test set-cookie simple.bru | 27 ++++++++++++++++ .../collection/environments/Local.bru | 11 +++++-- .../collection/environments/Prod.bru | 9 +++++- packages/bruno-tests/package.json | 1 + packages/bruno-tests/src/echo/index.js | 18 ++++++++++- packages/bruno-tests/src/index.js | 31 ++++++++++++++++--- packages/bruno-tests/ssl/localhost.crt | 20 ++++++++++++ packages/bruno-tests/ssl/localhost.key | 28 +++++++++++++++++ 17 files changed, 339 insertions(+), 8 deletions(-) create mode 100644 packages/bruno-tests/collection/cookies/folder.bru create mode 100644 packages/bruno-tests/collection/cookies/test secure+https,http/folder.bru create mode 100644 packages/bruno-tests/collection/cookies/test secure+https,http/set and verify.bru create mode 100644 packages/bruno-tests/collection/cookies/test secure+https,http/verify the cookie with http.bru create mode 100644 packages/bruno-tests/collection/cookies/test secure+local-ipv4+http.bru create mode 100644 packages/bruno-tests/collection/cookies/test secure+local-ipv4+https.bru create mode 100644 packages/bruno-tests/collection/cookies/test secure+localhost+http.bru create mode 100644 packages/bruno-tests/collection/cookies/test secure+localhost+https.bru create mode 100644 packages/bruno-tests/collection/cookies/test secure+non-local+http.bru create mode 100644 packages/bruno-tests/collection/cookies/test set-cookie simple.bru create mode 100644 packages/bruno-tests/ssl/localhost.crt create mode 100644 packages/bruno-tests/ssl/localhost.key diff --git a/packages/bruno-tests/collection/cookies/folder.bru b/packages/bruno-tests/collection/cookies/folder.bru new file mode 100644 index 000000000..39117b0e6 --- /dev/null +++ b/packages/bruno-tests/collection/cookies/folder.bru @@ -0,0 +1,4 @@ +meta { + name: cookies + seq: 13 +} diff --git a/packages/bruno-tests/collection/cookies/test secure+https,http/folder.bru b/packages/bruno-tests/collection/cookies/test secure+https,http/folder.bru new file mode 100644 index 000000000..03ba131e4 --- /dev/null +++ b/packages/bruno-tests/collection/cookies/test secure+https,http/folder.bru @@ -0,0 +1,4 @@ +meta { + name: test secure+https,http + seq: 3 +} diff --git a/packages/bruno-tests/collection/cookies/test secure+https,http/set and verify.bru b/packages/bruno-tests/collection/cookies/test secure+https,http/set and verify.bru new file mode 100644 index 000000000..99e0693e1 --- /dev/null +++ b/packages/bruno-tests/collection/cookies/test secure+https,http/set and verify.bru @@ -0,0 +1,31 @@ +meta { + name: set and verify + type: http + seq: 2 +} + +post { + url: {{remote_host}}/api/echo/custom + body: json + auth: inherit +} + +body:json { + { + "type": "text/plain", + "headers": { + "set-cookie": [ + "secure-https=val; Secure", + "nosecure-https-key2=val" + ], + "location": "/api/echo/trace" + }, + "statusCode": 302, + "content": "hello" + } +} + +assert { + res.body.headers.cookie: contains secure-https=val + res.body.headers.cookie: contains nosecure-https-key2=val +} diff --git a/packages/bruno-tests/collection/cookies/test secure+https,http/verify the cookie with http.bru b/packages/bruno-tests/collection/cookies/test secure+https,http/verify the cookie with http.bru new file mode 100644 index 000000000..9fab959a9 --- /dev/null +++ b/packages/bruno-tests/collection/cookies/test secure+https,http/verify the cookie with http.bru @@ -0,0 +1,16 @@ +meta { + name: verify the cookie with http + type: http + seq: 3 +} + +post { + url: {{remote_host_http}}/api/echo/trace + body: none + auth: inherit +} + +assert { + res.body.headers.cookie: notContains secure-https=val + res.body.headers.cookie: contains nosecure-https-key2=val +} diff --git a/packages/bruno-tests/collection/cookies/test secure+local-ipv4+http.bru b/packages/bruno-tests/collection/cookies/test secure+local-ipv4+http.bru new file mode 100644 index 000000000..dbd06465e --- /dev/null +++ b/packages/bruno-tests/collection/cookies/test secure+local-ipv4+http.bru @@ -0,0 +1,29 @@ +meta { + name: test secure+local-ipv4+http + type: http + seq: 2 +} + +post { + url: http://127.0.0.1:{{local_http_port}}/api/echo/custom + body: json + auth: inherit +} + +body:json { + { + "type": "text/plain", + "headers": { + "set-cookie": [ + "secure-local-ipv4-http=val; Secure" + ], + "location": "/api/echo/trace" + }, + "statusCode": 302, + "content": "hello" + } +} + +assert { + res.body.headers.cookie: contains secure-local-ipv4-http=val +} diff --git a/packages/bruno-tests/collection/cookies/test secure+local-ipv4+https.bru b/packages/bruno-tests/collection/cookies/test secure+local-ipv4+https.bru new file mode 100644 index 000000000..2727f1534 --- /dev/null +++ b/packages/bruno-tests/collection/cookies/test secure+local-ipv4+https.bru @@ -0,0 +1,29 @@ +meta { + name: test secure+local-ipv4+https + type: http + seq: 2 +} + +post { + url: https://127.0.0.1:{{local_https_port}}/api/echo/custom + body: json + auth: inherit +} + +body:json { + { + "type": "text/plain", + "headers": { + "set-cookie": [ + "secure-local-ipv4-https=val; Secure" + ], + "location": "/api/echo/trace" + }, + "statusCode": 302, + "content": "hello" + } +} + +assert { + res.body.headers.cookie: contains secure-local-ipv4-https=val +} diff --git a/packages/bruno-tests/collection/cookies/test secure+localhost+http.bru b/packages/bruno-tests/collection/cookies/test secure+localhost+http.bru new file mode 100644 index 000000000..ea11bba4d --- /dev/null +++ b/packages/bruno-tests/collection/cookies/test secure+localhost+http.bru @@ -0,0 +1,29 @@ +meta { + name: test secure+localhost+http + type: http + seq: 2 +} + +post { + url: http://localhost:{{local_http_port}}/api/echo/custom + body: json + auth: inherit +} + +body:json { + { + "type": "text/plain", + "headers": { + "set-cookie": [ + "secure-localhost-http=val; Secure" + ], + "location": "/api/echo/trace" + }, + "statusCode": 302, + "content": "hello" + } +} + +assert { + res.body.headers.cookie: contains secure-localhost-http=val +} diff --git a/packages/bruno-tests/collection/cookies/test secure+localhost+https.bru b/packages/bruno-tests/collection/cookies/test secure+localhost+https.bru new file mode 100644 index 000000000..8008da224 --- /dev/null +++ b/packages/bruno-tests/collection/cookies/test secure+localhost+https.bru @@ -0,0 +1,29 @@ +meta { + name: test secure+localhost+https + type: http + seq: 2 +} + +post { + url: https://localhost:{{local_https_port}}/api/echo/custom + body: json + auth: inherit +} + +body:json { + { + "type": "text/plain", + "headers": { + "set-cookie": [ + "secure-localhost-https=val; Secure" + ], + "location": "/api/echo/trace" + }, + "statusCode": 302, + "content": "hello" + } +} + +assert { + res.body.headers.cookie: contains secure-localhost-https=val +} diff --git a/packages/bruno-tests/collection/cookies/test secure+non-local+http.bru b/packages/bruno-tests/collection/cookies/test secure+non-local+http.bru new file mode 100644 index 000000000..9aacba217 --- /dev/null +++ b/packages/bruno-tests/collection/cookies/test secure+non-local+http.bru @@ -0,0 +1,31 @@ +meta { + name: test secure+non-local+http + type: http + seq: 2 +} + +post { + url: http://internal:{{local_http_port}}/api/echo/custom + body: json + auth: inherit +} + +body:json { + { + "type": "text/plain", + "headers": { + "set-cookie": [ + "secure-non-local-http=val; Secure", + "secure-non-local-http-key2=val" + ], + "location": "/api/echo/trace" + }, + "statusCode": 302, + "content": "hello" + } +} + +assert { + res.body.headers.cookie: notContains secure-non-local-http=val + res.body.headers.cookie: contains secure-non-local-http-key2=val +} diff --git a/packages/bruno-tests/collection/cookies/test set-cookie simple.bru b/packages/bruno-tests/collection/cookies/test set-cookie simple.bru new file mode 100644 index 000000000..db2c73c65 --- /dev/null +++ b/packages/bruno-tests/collection/cookies/test set-cookie simple.bru @@ -0,0 +1,27 @@ +meta { + name: test set-cookie simple + type: http + seq: 1 +} + +post { + url: {{host}}/api/echo/custom + body: json + auth: inherit +} + +body:json { + { + "type": "text/plain", + "headers": { + "set-cookie": "simple-cookie=val", + "location": "/api/echo/trace" + }, + "statusCode": 302, + "content": "hello" + } +} + +assert { + res.body.headers.cookie: contains simple-cookie=val +} diff --git a/packages/bruno-tests/collection/environments/Local.bru b/packages/bruno-tests/collection/environments/Local.bru index a7ac3f541..a5ebb4da7 100644 --- a/packages/bruno-tests/collection/environments/Local.bru +++ b/packages/bruno-tests/collection/environments/Local.bru @@ -1,6 +1,13 @@ vars { - host: http://localhost:8080 - httpfaker: https://www.httpfaker.org + host: {{local_host}} + host_http: {{local_host_http}} + httpfaker: {{host}} + local_http_port: 8081 + local_https_port: 8082 + local_host: https://localhost:{{local_https_port}} + local_host_http: http://localhost:{{local_http_port}} + remote_host: https://testbench-sanity.usebruno.com + remote_host_http: http://testbench-sanity.usebruno.com bearer_auth_token: your_secret_token basic_auth_password: della env.var1: envVar1 diff --git a/packages/bruno-tests/collection/environments/Prod.bru b/packages/bruno-tests/collection/environments/Prod.bru index f33c1bb05..294ef8338 100644 --- a/packages/bruno-tests/collection/environments/Prod.bru +++ b/packages/bruno-tests/collection/environments/Prod.bru @@ -1,6 +1,13 @@ vars { - host: https://testbench-sanity.usebruno.com + host: {{remote_host}} + host_http: {{remote_host}} httpfaker: https://www.httpfaker.org + local_http_port: 8081 + local_https_port: 8082 + local_host: https://localhost:{{local_https_port}} + local_host_http: http://localhost:{{local_http_port}} + remote_host: https://testbench-sanity.usebruno.com + remote_host_http: http://testbench-sanity.usebruno.com bearer_auth_token: your_secret_token basic_auth_password: della env.var1: envVar1 diff --git a/packages/bruno-tests/package.json b/packages/bruno-tests/package.json index 75cfde1ae..8f638ff2e 100644 --- a/packages/bruno-tests/package.json +++ b/packages/bruno-tests/package.json @@ -4,6 +4,7 @@ "description": "", "main": "src/index.js", "scripts": { + "dev": "NODE_ENV=development node .", "start": "node ." }, "repository": { diff --git a/packages/bruno-tests/src/echo/index.js b/packages/bruno-tests/src/echo/index.js index 00b50bd36..b0ca416a2 100644 --- a/packages/bruno-tests/src/echo/index.js +++ b/packages/bruno-tests/src/echo/index.js @@ -49,10 +49,14 @@ router.get('/iso-enc', (req, res) => { }); router.post("/custom", (req, res) => { - const { headers, content, contentBase64, contentJSON, type } = req.body || {}; + const { headers, content, contentBase64, contentJSON, type, statusCode } = req.body || {}; res._headers = {}; + if (statusCode) { + res.status(statusCode); + } + if (type) { res.setHeader('Content-Type', type); } @@ -74,4 +78,16 @@ router.post("/custom", (req, res) => { return res.end(); }); +router.all('/trace', (req, res) => { + const requestDetails = { + url: req.url, + method: req.method, + query: req.query, + headers: req.headers, + body: req.body, + rawBody: req.rawBody + }; + res.json(requestDetails); +}); + module.exports = router; diff --git a/packages/bruno-tests/src/index.js b/packages/bruno-tests/src/index.js index 735bd929b..19e22d46d 100644 --- a/packages/bruno-tests/src/index.js +++ b/packages/bruno-tests/src/index.js @@ -6,9 +6,18 @@ const authRouter = require('./auth'); const echoRouter = require('./echo'); const xmlParser = require('./utils/xmlParser'); const multipartRouter = require('./multipart'); - const app = new express(); -const port = process.env.PORT || 8081; +const httpPort = process.env.HTTP_PORT || 8081; + +let requestCounter = 0; +app.use((req, res, next) => { + const requestId = ++requestCounter; + console.log(`Request [${requestId}]: ${req.method} ${req.originalUrl}`); + res.on('finish', () => { + console.log(`Response [${requestId}]: ${res.statusCode}`); + }); + next(); +}); app.use(cors()); @@ -45,6 +54,20 @@ app.get('/redirect-to-ping', function (req, res) { return res.redirect('/ping'); }); -app.listen(port, function () { - console.log(`Testbench started on port: ${port}`); +app.listen(httpPort, function () { + console.log(`Testbench started on port: ${httpPort}(HTTP)`); }); + +if (process.env.NODE_ENV === 'development') { + const https = require('https'); + const fs = require('fs'); + const path = require('path'); + const httpsPort = process.env.HTTPS_PORT || 8082; + const sslOptions = { + key: fs.readFileSync(path.join(__dirname, '../ssl/localhost.key')), + cert: fs.readFileSync(path.join(__dirname, '../ssl/localhost.crt')) + }; + https.createServer(sslOptions, app).listen(httpsPort, function () { + console.log(`Testbench started on port: ${httpsPort}(HTTPS)`); + }); +} diff --git a/packages/bruno-tests/ssl/localhost.crt b/packages/bruno-tests/ssl/localhost.crt new file mode 100644 index 000000000..8c3b25188 --- /dev/null +++ b/packages/bruno-tests/ssl/localhost.crt @@ -0,0 +1,20 @@ +-----BEGIN CERTIFICATE----- +MIIDVDCCAjygAwIBAgIUTnHvaGqAMWT2r5kb1A7gJiY3ZUowDQYJKoZIhvcNAQEL +BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTI1MDUxOTA5MzAwMVoXDTM1MDUx +NzA5MzAwMVowFDESMBAGA1UEAwwJbG9jYWxob3N0MIIBIjANBgkqhkiG9w0BAQEF +AAOCAQ8AMIIBCgKCAQEAm4Gt9rW0Ir4PgpLGGWzbhbSC/swPql6tb3R2jirWddjl +o/vq/22+P9Xu47SAlj9N3TcETANfrazATVWzjjm5GtSfWFSgVqW1dCRQ1J2FqESP +279MM5tfqfhBx3Mtsg+K9F4nLTooG9zDhbPx8X0zua4b8HObH3fW0iOQRyiVIUQj +vzCaPjulOrjPxrUuY9hNm+sdmI12UEYY0IGcxhLDs8AFGUQDp/u56BALh8T/91gh +TJQwOQZIFwfUTK3iIIKg4sRvr71yqE485R4F4JTZfIizcKHOhmp3LxBrfQ/OX1Xe +D4dcG2I8Q7BnjxTJIEIsWx0XyKqPy4Wm5HEe+UnaGwIDAQABo4GdMIGaMB0GA1Ud +DgQWBBRXh0x2qA9vCfXozDPG4e2zhXtN+jAfBgNVHSMEGDAWgBRXh0x2qA9vCfXo +zDPG4e2zhXtN+jAPBgNVHRMBAf8EBTADAQH/MEcGA1UdEQRAMD6CCWxvY2FsaG9z +dIIQKi5zdWIxLmxvY2FsaG9zdIIIaW50ZXJuYWyCDyouc3ViMS5pbnRlcm5hbIcE +fwAAATANBgkqhkiG9w0BAQsFAAOCAQEAHdGnVwSttfBhbhKTekI3+o1hYIu7EsUu +NtX6+ZLXWpb2NInYEFMgfKOCpOzRLoh8IfPVolN/3x0N/Zr6PY7WAGp2aPXBumw9 +OwPyz9rBHHpXVh2l5OHyhXWTSmyc18xRZdqFfm/uskvH6LIAGOy4b7buPAxBXpeU +LjJfvXLg9vjhoMWyaBDvMGjwopDvrslDD7ZK5R4SjmbsTFvJ304zBTdx3TMCMXpz +rWP2iR+qrRab4prXHCrRnAEc+SYMyCIld3sm6mqdTuohXbEpWDTpulhuvYs3Csmz +m+RTSQ+1NjsEqtkAz82PRl6HPjGaHCivxkQEAdE/x3Lh5LmkfgZCVQ== +-----END CERTIFICATE----- diff --git a/packages/bruno-tests/ssl/localhost.key b/packages/bruno-tests/ssl/localhost.key new file mode 100644 index 000000000..adaebea83 --- /dev/null +++ b/packages/bruno-tests/ssl/localhost.key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCbga32tbQivg+C +ksYZbNuFtIL+zA+qXq1vdHaOKtZ12OWj++r/bb4/1e7jtICWP03dNwRMA1+trMBN +VbOOObka1J9YVKBWpbV0JFDUnYWoRI/bv0wzm1+p+EHHcy2yD4r0XictOigb3MOF +s/HxfTO5rhvwc5sfd9bSI5BHKJUhRCO/MJo+O6U6uM/GtS5j2E2b6x2YjXZQRhjQ +gZzGEsOzwAUZRAOn+7noEAuHxP/3WCFMlDA5BkgXB9RMreIggqDixG+vvXKoTjzl +HgXglNl8iLNwoc6GancvEGt9D85fVd4Ph1wbYjxDsGePFMkgQixbHRfIqo/Lhabk +cR75SdobAgMBAAECggEAFUfpOgqiZ4wXAfQlbrP6VBE19A88/ZW4qmNdGMDoBHdK +8JFjFtLlPEXXTAtsUCNtpnOxqi1er0fQMUUkg2GG4WhuPgpRaZ+Cdi3ExLP3PJCA +F2nV0HeCNKJ9bgFnJCets8TGxmZfuCBoFCi3WChIJzBFMTdrkneLSyAEI0N8f8a6 +yodjLYvApM+nXj8vibFONdyqEPno41rbQQQXtYHwGm1l+q29sAstEqC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