From a1d3678aeed364970736592719176d9d6894b934 Mon Sep 17 00:00:00 2001
From: Khafra <maitken033380023@gmail.com>
Date: Wed, 5 Apr 2023 00:34:51 -0400
Subject: [PATCH] fix: set window option properly (#2048)

* fix: set window option properly

* Update lib/fetch/request.js

Co-authored-by: Robert Nagy <ronagy@icloud.com>

* Update lib/fetch/request.js

Co-authored-by: Robert Nagy <ronagy@icloud.com>

* add test

---------

Co-authored-by: Robert Nagy <ronagy@icloud.com>
---
 lib/fetch/request.js                     |  2 +-
 test/fetch/407-statuscode-window-null.js | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 test/fetch/407-statuscode-window-null.js

diff --git a/lib/fetch/request.js b/lib/fetch/request.js
index f3d63cca016..d6263fd2d43 100644
--- a/lib/fetch/request.js
+++ b/lib/fetch/request.js
@@ -133,7 +133,7 @@ class Request {
     }
 
     // 11. If init["window"] exists, then set window to "no-window".
-    if (init.window !== undefined) {
+    if ('window' in init) {
       window = 'no-window'
     }
 
diff --git a/test/fetch/407-statuscode-window-null.js b/test/fetch/407-statuscode-window-null.js
new file mode 100644
index 00000000000..e22554fac15
--- /dev/null
+++ b/test/fetch/407-statuscode-window-null.js
@@ -0,0 +1,20 @@
+'use strict'
+
+const { fetch } = require('../..')
+const { createServer } = require('http')
+const { once } = require('events')
+const { test } = require('tap')
+
+test('Receiving a 407 status code w/ a window option present should reject', async (t) => {
+  const server = createServer((req, res) => {
+    res.statusCode = 407
+    res.end()
+  }).listen(0)
+
+  t.teardown(server.close.bind(server))
+  await once(server, 'listening')
+
+  // if init.window exists, the spec tells us to set request.window to 'no-window',
+  // which later causes the request to be rejected if the status code is 407
+  await t.rejects(fetch(`http://localhost:${server.address().port}`, { window: null }))
+})