github.com/pbberlin/tools@v0.0.0-20160910141205-7aa5421c2169/os/fsi/tests/http_fs_test.go (about)

     1  // +build http
     2  // go test -tags=http
     3  
     4  package tests
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"log"
    10  	"net/http"
    11  	"os"
    12  	"runtime"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/pbberlin/tools/os/fsi/httpfs"
    17  )
    18  
    19  func TestHttp(t *testing.T) {
    20  
    21  	testRoot := "c:\\temp"
    22  	if runtime.GOOS != "windows" {
    23  		testRoot = "/tmp"
    24  	}
    25  
    26  	bb := new(bytes.Buffer)
    27  	msg := ""
    28  	_ = bb
    29  
    30  	os.Chdir(testRoot)
    31  	pwd, _ := os.Getwd()
    32  	if pwd == testRoot {
    33  		// os.RemoveAll(pwd)
    34  	}
    35  
    36  	Fss, c := initFileSystems()
    37  	defer c.Close()
    38  
    39  	// Fss = Fss[0:1]
    40  	// Fss = Fss[1:2]
    41  	// Fss = Fss[2:3]
    42  
    43  	portInc := 2
    44  	for _, fs := range Fss {
    45  
    46  		wpf(os.Stdout, "-----created fs %v %v-----\n", fs.Name(), fs.String())
    47  
    48  		bb, msg = CreateSys(fs)
    49  		if msg != "" {
    50  			wpf(os.Stdout, msg+"\n")
    51  			wpf(os.Stdout, bb.String())
    52  			t.Errorf("%v", msg)
    53  		}
    54  
    55  		httpFSys := &httpfs.HttpFs{SourceFs: fs}
    56  
    57  		mux := http.NewServeMux()
    58  
    59  		//
    60  		fileserver1 := http.FileServer(httpFSys.Dir("./"))
    61  		mux.Handle("/", fileserver1)
    62  
    63  		//
    64  		fileserver2 := http.FileServer(http.Dir("./css/"))
    65  		mux.Handle("/css/", http.StripPrefix("/css/", fileserver2))
    66  
    67  		go func(arg int) {
    68  			addr := fmt.Sprintf("localhost:400%v", arg)
    69  			log.Printf("serving %v\n", addr)
    70  			log.Fatal(http.ListenAndServe(addr, mux))
    71  		}(portInc)
    72  
    73  		portInc++
    74  
    75  	}
    76  
    77  	time.Sleep(time.Second * 2111)
    78  
    79  }