github.com/vugu/vugu@v0.3.6-0.20240430171613-3f6f402e014b/simplehttp/simple-handler_test.go (about)

     1  package simplehttp
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestSimpleHandlerDev(t *testing.T) {
    15  
    16  	assert := assert.New(t)
    17  
    18  	tmpDir, err := os.MkdirTemp("", "TestSimpleHandler")
    19  	assert.NoError(err)
    20  	// log.Printf("tmpDir = %q", tmpDir)
    21  	defer os.RemoveAll(tmpDir)
    22  
    23  	wd, _ := os.Getwd()
    24  	vugudir, _ := filepath.Abs(filepath.Join(wd, ".."))
    25  
    26  	// write a go.mod that points vugu module to use our local path instead of pulling from github
    27  	assert.NoError(os.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(`
    28  module example.com/test
    29  replace github.com/vugu/vugu => `+vugudir+`
    30  	`), 0644))
    31  
    32  	assert.NoError(os.WriteFile(filepath.Join(tmpDir, "root.vugu"), []byte(`
    33  <div>I Am Root</div>
    34  `), 0644))
    35  
    36  	assert.NoError(os.WriteFile(filepath.Join(tmpDir, "test.js"), []byte(`
    37  // test.js here
    38  `), 0644))
    39  
    40  	h := New(tmpDir, true)
    41  	srv := httptest.NewServer(h)
    42  	defer srv.Close()
    43  
    44  	assert.Contains(mustGetPage(srv.URL+"/"), "<body")                      // index page
    45  	assert.Contains(mustGetPage(srv.URL+"/other-page"), "<body")            // other HTML page
    46  	assert.Contains(mustGetPage(srv.URL+"/test.js"), "// test.js here")     // static file
    47  	assert.Contains(mustGetPage(srv.URL+"/wasm_exec.js"), "globalThis.Go")  // Go WASM support js file
    48  	assert.Contains(mustGetPage(srv.URL+"/does-not-exist.js"), "not found") // other misc not found file
    49  	assert.Contains(mustGetPage(srv.URL+"/main.wasm"), "\x00asm")           // WASM binary should have marker
    50  
    51  }
    52  
    53  func TestSimpleHandlerProd(t *testing.T) {
    54  
    55  	assert := assert.New(t)
    56  
    57  	tmpDir, err := os.MkdirTemp("", "TestSimpleHandler")
    58  	assert.NoError(err)
    59  	// log.Printf("tmpDir = %q", tmpDir)
    60  	defer os.RemoveAll(tmpDir)
    61  
    62  	wd, _ := os.Getwd()
    63  	vugudir, _ := filepath.Abs(filepath.Join(wd, ".."))
    64  
    65  	// write a go.mod that points vugu module to use our local path instead of pulling from github
    66  	assert.NoError(os.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte(`
    67  module example.com/test
    68  replace github.com/vugu/vugu => `+vugudir+`
    69  	`), 0644))
    70  
    71  	assert.NoError(os.WriteFile(filepath.Join(tmpDir, "root.vugu"), []byte(`
    72  <div>I Am Root</div>
    73  `), 0644))
    74  
    75  	assert.NoError(os.WriteFile(filepath.Join(tmpDir, "test.js"), []byte(`
    76  // test.js here
    77  `), 0644))
    78  
    79  	h := New(tmpDir, false)
    80  	srv := httptest.NewServer(h)
    81  	defer srv.Close()
    82  
    83  	assert.Contains(mustGetPage(srv.URL+"/"), "<body")                      // index page
    84  	assert.Contains(mustGetPage(srv.URL+"/other-page"), "<body")            // other HTML page
    85  	assert.Contains(mustGetPage(srv.URL+"/test.js"), "// test.js here")     // static file
    86  	assert.Contains(mustGetPage(srv.URL+"/wasm_exec.js"), "not found")      // Go WASM support js file
    87  	assert.Contains(mustGetPage(srv.URL+"/does-not-exist.js"), "not found") // other misc not found file
    88  	assert.Contains(mustGetPage(srv.URL+"/main.wasm"), "not found")         // WASM binary should have marker
    89  
    90  }
    91  
    92  func mustGetPage(u string) string {
    93  	res, err := http.Get(u)
    94  	if err != nil {
    95  		panic(err)
    96  	}
    97  	defer res.Body.Close()
    98  	b, err := io.ReadAll(res.Body)
    99  	if err != nil {
   100  		panic(err)
   101  	}
   102  	return string(b)
   103  }