tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_channel_creation.js (2876B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 /**
      7 * Test that channels are created with correct properties.
      8 */
      9 add_task(async function test_channel_creation() {
     10  const testURI = createTestOHTTPResourceURI("https://example.com/image.jpg");
     11  const channel = createTestChannel(testURI);
     12 
     13  Assert.ok(channel, "Channel should be created");
     14  Assert.equal(channel.URI.spec, testURI, "Channel URI should match input");
     15  Assert.equal(channel.originalURI.spec, testURI, "Original URI should match");
     16  Assert.equal(channel.loadFlags, 0, "Default load flags should be 0");
     17  Assert.equal(channel.contentType, "", "Default content type should be empty");
     18  Assert.equal(
     19    channel.contentLength,
     20    -1,
     21    "Default content length should be -1"
     22  );
     23  Assert.equal(channel.status, Cr.NS_OK, "Default status should be NS_OK");
     24 });
     25 
     26 /**
     27 * Test that synchronous open throws an error.
     28 */
     29 add_task(async function test_synchronous_open_not_supported() {
     30  const channel = createTestChannel(
     31    createTestOHTTPResourceURI("https://example.com/image.jpg")
     32  );
     33 
     34  Assert.throws(
     35    () => channel.open(),
     36    /moz-cached-ohttp protocol does not support synchronous open/,
     37    "Should throw error for synchronous open"
     38  );
     39 });
     40 
     41 /**
     42 * Test channel cancellation.
     43 */
     44 add_task(async function test_channel_cancellation() {
     45  const channel = createTestChannel(
     46    createTestOHTTPResourceURI("https://example.com/image.jpg")
     47  );
     48 
     49  // Cancel the channel
     50  channel.cancel(Cr.NS_ERROR_ABORT);
     51 
     52  Assert.equal(channel.status, Cr.NS_ERROR_ABORT, "Status should be updated");
     53  Assert.equal(
     54    channel.isPending(),
     55    false,
     56    "Should not be pending after cancel"
     57  );
     58 
     59  // Try to open cancelled channel
     60  Assert.throws(
     61    () => {
     62      channel.asyncOpen({
     63        onStartRequest() {},
     64        onDataAvailable() {},
     65        onStopRequest() {},
     66      });
     67    },
     68    /Channel was cancelled/,
     69    "Should throw error when opening cancelled channel"
     70  );
     71 });
     72 
     73 /**
     74 * Test other channel properties.
     75 */
     76 add_task(async function test_channel_properties() {
     77  const channel = createTestChannel(
     78    createTestOHTTPResourceURI("https://example.com/image.jpg")
     79  );
     80 
     81  Assert.ok(channel.QueryInterface(Ci.nsIChannel));
     82 
     83  // Test content type
     84  channel.contentType = "image/png";
     85  Assert.equal(channel.contentType, "image/png", "Should update content type");
     86 
     87  // Test content length
     88  channel.contentLength = 2048;
     89  Assert.equal(channel.contentLength, 2048, "Should update content length");
     90 
     91  // Test load flags
     92  channel.loadFlags = Ci.nsIRequest.LOAD_BYPASS_CACHE;
     93  Assert.equal(
     94    channel.loadFlags,
     95    Ci.nsIRequest.LOAD_BYPASS_CACHE,
     96    "Should update load flags"
     97  );
     98 
     99  // Test name property
    100  Assert.equal(
    101    channel.name,
    102    channel.URI.spec,
    103    "Channel name should match URI spec"
    104  );
    105 });