How to remove headers like Authorization while using playwright apiContext

  1. My project implemented the Authorization as a fixture so that all test modules can work as a logged-in user.

  2. There is a function to upload a file to s3, which actually consists of a few steps:

  • initiate upload. It will return the s3 upload_url and something else.
  • upload to s3. It will use the upload_url and disallow Authorization.

Error message:
InvalidArgumentOnly one auth mechanism allowed; only the X-Amz-Algorithm query parameter, Signature query string parameter or the Authorization header should be specified

I tried to set the headers to empty in my function, but it doesn’t work. Can anyone please help me? Do I must add the browserContext and use the route() function?

export const demandTags = async (
  apiContext,
  campaign,
  campaignLinesDetails: CampaignLineResponse[],
  demandTagsDetails: DemandTagRequest[],
  use: (r: DemandTagResponse[]) => Promise<void>,
) => {
  const results: DemandTagResponse[] = [];

  // Get uploadCreative.mp4 from the files
  const filePromise = new Promise((resolve, reject) => {
    readFile('./uploadItems/uploadCreative.mp4', (err, data) => {
      if (err) {
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const file: any = await filePromise;

  // Initiate upload
  const initiateUpload = await apiContext.post('/api/demandtag/initiate_upload/', {
    data: {
      file_name: 'uploadCreative.mp4',
      file_type: 'video/mp4',
      file_size: file.length,
    } as apiTypes.UploadRequest,
  });
  await expect(initiateUpload).toBeOK();

  const responseJSONinitiateUpload = await initiateUpload.json();
  expect(responseJSONinitiateUpload.parts[0].offset).toBe(0);
  expect(responseJSONinitiateUpload.parts[0].limit).toBe(file.length);
  expect(responseJSONinitiateUpload.parts).toHaveLength(1);

  // Upload the part
  const partResponse = await apiContext.put(responseJSONinitiateUpload.parts[0].url, {
    headers: {
      // 'Content-Type': '',
    },
    data: file,
  });
  await expect(partResponse).toBeOK();

  const etag = await partResponse.headers()['etag'];
  const completeUpload = await apiContext.post('/api/demandtag/complete_upload/', {
    data: {
      upload_token: responseJSONinitiateUpload.upload_token,
      etags: [etag],
    } as apiTypes.CompleteUploadRequest,
  });
  await expect(completeUpload).toBeOK();

  const responseJSONcompleteUpload = await completeUpload.json();

  // Create demand tag
  for (const demandTagDetails of demandTagsDetails) {
    const createDemandTag = await apiContext.post('/api/demandtag/', {
      data: {
        name: demandTagDetails.name,
        budget_value: demandTagDetails.budgetValue,
        campaign_line: campaignLinesDetails[0].id,
        pixel_url: demandTagDetails.pixelUrl,
        creative_landing_page_url: demandTagDetails.creativeLandingPageUrl,
        creative_s3: responseJSONcompleteUpload.url,
        creative_slug: demandTagDetails.creativeSlug,
        start_date: demandTagDetails.startDate,
        end_date: demandTagDetails.endDate,
      } as apiTypes.DemandTagRequest,
    });

    await expect(createDemandTag).toBeOK();
    results.push({ ...demandTagDetails, id: (await createDemandTag.json()).id });
  }

  await use(results);

  // Delete created demand tags
  for (const demandTag of results) {
    const responseDelete = await apiContext.delete(`/api/demandtag/${demandTag.id}/`);
    expect([204, 404]).toContain(responseDelete.status());
  }
};

The fixture defined in another file:

export const test = base.extend<TestFixtures, WorkerFixtures>({
  authToken: async ({ request }, use) => {
    await authToken(request, use);
  },
  apiContext: async ({ authToken }, use) => {
    await use(
      await request.newContext({
        baseURL: `${process.env.PROTOCOL!}://${process.env.HOST!}:${process.env.PORT!}`,
        extraHTTPHeaders: {
          authorization: `Token ${authToken}`,
        },
      }),
    );
  },
  staticUrl: [
    async ({}, use) => {
      await staticUrlFixture(use);
    },
    { scope: 'worker' },
  ],
});

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật