http_header_api.mx raw

   1  //:wasmtest skip
   2  // Native-only by COST, measured, not by failure: linking net/http for -target wasm
   3  // makes this fixture the most expensive one in the tree - its native and wasm builds
   4  // together take ~35 minutes, and tests/wasm/validate.sh wipes the per-compiler package
   5  // cache at the start of every run on purpose ("each run measures a cold compile"),
   6  // so the cost is paid on EVERY phase-5 run. It is not hidden: this fixture DOES
   7  // validate in the browser - gojs stubs and all - when run directly, which is how the
   8  // host capability was proved in round 108:
   9  //
  10  //     MOXIE=./moxie bash tests/wasm/validate.sh tests/regressions/should_compile/http_header_api.mx
  11  //
  12  // Header behaviour natively is covered every run (phase 0), and net/http COMPILING for
  13  // wasm is covered every run (phase 1c).
  14  /*
  15  http.Header is a named map type, and the whole API works through its methods.
  16  
  17  `type Header map[string][][]byte` carries Add, Set, Get, Values, Del, Clone,
  18  Write and WriteSubset as methods. A value receiver over a named map is a
  19  reference receiver, so the mutations reach the caller. The NAME survives: a
  20  Header is written Header{...}, converted with Header(...) and read with
  21  h[key], and the Request/Response fields are unchanged.
  22  
  23  This is a MAIN-package fixture deliberately: an unresolved call inside a library
  24  package's body is dropped SILENTLY (round 99), so only a main package proves the
  25  free functions exist. It pins canonicalization, first-value Get, all-values,
  26  Del, Clone independence, and both writers.
  27  
  28  NATIVE-ONLY, and the reason is the TARGET, not this fixture: linking net/http
  29  for `-target wasm` pulls its `js && wasm` files, so the module imports the
  30  `gojs` bridge, and the standalone-wasm validator host does not provide that
  31  module - the browser run dies at instantiation with
  32  
  33      WebAssembly.instantiate(): Import #18 "gojs": module is not an object or function
  34  
  35  That is true of ANY fixture importing net/http; net/http's wasm COMPILATION is
  36  covered by phase 1c (`wasm:net/http PASS`), and this fixture covers the API
  37  behaviour natively. Recorded in the work log.
  38  */
  39  package main
  40  
  41  import (
  42  	"bytes"
  43  	"net/http"
  44  )
  45  
  46  func main() {
  47  	// Termination: a batch program must still be stoppable, so the entry
  48  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  49  	select {
  50  	case <-shutdown:
  51  		return
  52  	default:
  53  	}
  54  
  55  	h := http.Header{}
  56  	h.Set("Content-Type", "text/plain")
  57  	h.Add("X-Multi", "one")
  58  	h.Add("X-Multi", "two")
  59  	if h.Get("content-type") != "text/plain" {
  60  		panic("get canonicalized key")
  61  	}
  62  	vs := h.Values("x-multi")
  63  	if len(vs) != 2 || string(vs[1]) != "two" {
  64  		panic("values keeps every value in order")
  65  	}
  66  	c := h.Clone()
  67  	c.Del("X-Multi")
  68  	if c.Get("x-multi") != "" {
  69  		panic("del removes every value")
  70  	}
  71  	if h.Get("x-multi") == "" {
  72  		panic("clone must be independent of the original")
  73  	}
  74  	var buf bytes.Buffer
  75  	if err := h.Write(&buf); err != nil {
  76  		panic("write")
  77  	}
  78  	if buf.Len() == 0 {
  79  		panic("write produced nothing")
  80  	}
  81  	var buf2 bytes.Buffer
  82  	if err := h.WriteSubset(&buf2, map[string]bool{"X-Multi": true}); err != nil {
  83  		panic("writesubset")
  84  	}
  85  	println("header ok")
  86  }
  87