You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cypress测试访问Azure Blob Storage时出现403认证错误求助

Cypress访问Azure Blob Storage音频片段时出现403认证错误

我在使用Cypress测试时,尝试访问托管在Azure Blob Storage上的应用中的音频片段,持续收到以下错误:

"403 Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature."

手动通过浏览器使用该应用一切正常,但Cypress运行时请求失败。

我的Cypress配置如下:

const { defineConfig } = require("cypress");

module.exports = defineConfig({
  e2e: {
    baseUrl: 'https://tts-sa-dev.azurewebsites.net',
    setupNodeEvents(on, config) {
      // implement node event listeners here 
    },
  },
});

我在每个测试套件前使用Basic认证:

beforeEach(() => cy.authenticate('basic')); 

认证命令的实现如下:

Cypress.Commands.add('authenticate', (authType: 'basic') => {
  if (authType === 'basic') {
    
    const username = 'XXXX'; 
    const password = 'XXXX'; 
    const authHeader = 'Basic ' + btoa(username + ':' + password);

    // Intercept all requests and add the Authorization header
    cy.intercept('**', (req) => {
      req.headers['Authorization'] = authHeader;
    });
  }
});

我怀疑该认证头可能与Azure的CORS签名存在冲突,但没在Stack Overflow找到类似问题。

我已将测试简化至最简形式,但调用接口后仍无法获取数据。我意识到Cypress可能修改了请求头,但拦截时未发现差异。是否有人遇到过类似问题?

我对比了请求头,发现存在一些差异:缺少Origin头、Accept为选项数组、fetch-mode为non-cors,但调整这些头后仍无效果。调整代码如下:

cy.intercept('*', (req) => {
      console.log('Request Headers at Intercept:', req.headers);
      req.headers['Accept'] = 'application/json';
      req.headers['Origin'] = 'https://xxxx.azurewebsites.net';
      req.headers['Sec-Fetch-Mode'] = 'cors';
      req.headers['Accept-Language'] = 'cs,en;q=0.9';
    });

内容的提问来源于stack exchange,提问作者Jakub Faist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 11:46:00