const http = require("http");

// Simulate what access-manager does - call oauth2-proxy auth endpoint
// with the cookie from the browser
const cookie = process.argv[2] || "";
const host = process.argv[3] || "docs.scottfelten.com";

console.log("Testing oauth2-proxy auth with:");
console.log("  Cookie length:", cookie.length);
console.log("  Host:", host);

const opts = {
  hostname: "127.0.0.1",
  port: 4180,
  path: "/oauth2/auth",
  method: "GET",
  headers: {
    Cookie: cookie,
    "X-Forwarded-Proto": "https",
    Host: host,
  },
};

const req = http.request(opts, (res) => {
  console.log("\noauth2-proxy response:");
  console.log("  Status:", res.statusCode);
  console.log("  Headers:", JSON.stringify(res.headers, null, 2));
  let body = "";
  res.on("data", (d) => (body += d));
  res.on("end", () => { if (body) console.log("  Body:", body); });
});

req.on("error", (e) => console.error("Error:", e.message));
req.end();
