Commits

Miki Tebeka committed 6b8cff7

test mask

  • Participants
  • Parent commits d550ca6
  • Branches mask

Comments (0)

Files changed (1)

File nrsc_test.go

 	port = 9888
 )
 
+func TestMask(t *testing.T) {
+	resp := getResp(t, "/static/i.gif")
+	if resp.StatusCode != http.StatusUnauthorized {
+		t.Fatalf("got masked resource - %d\n", resp.StatusCode)
+	}
+}
+
 func TestText(t *testing.T) {
 	expected := map[string]string{
 		"Content-Size": "12",
 }
 
 func fixGOPATH(cwd string) {
-	path := os.Getenv("GOPATH")
-	if len(path) == 0 {
-		os.Setenv("GOPATH", fmt.Sprintf("%s/../..", cwd))
-	}
+	os.Setenv("GOPATH", fmt.Sprintf("%s/../..", cwd))
 }
 
 func init() {
 	os.Chdir(root)
 	defer os.Chdir(cwd)
 
-	cmd := exec.Command("go", "install")
+	cmd := exec.Command("go", "install", "nrsc")
+	if err := cmd.Run(); err != nil {
+		fmt.Printf("error installing: %s\n", err)
+		panic(err)
+	}
+
+	cmd = exec.Command("go", "build")
 	if err := cmd.Run(); err != nil {
 		fmt.Printf("error building: %s\n", err)
 		panic(err)
 	}
 }
 
-func checkPath(t *testing.T, path string, expected map[string]string) {
+func getResp(t *testing.T, path string) *http.Response {
 	server := startServer(t)
 	if server == nil {
-		return
+		return nil
 	}
 	defer server.Process.Kill()
 
 	resp, err := get(path)
 	if err != nil {
 		t.Fatalf("%s\n", err)
+		return nil
 	}
 
+	return resp
+}
+
+func checkPath(t *testing.T, path string, expected map[string]string) {
+	resp := getResp(t, path)
 	if resp.StatusCode != http.StatusOK {
 		t.Fatalf("bad reply - %s", resp.Status)
 	}
 	"fmt"
 	"net/http"
 	"os"
+	"regexp"
 
 	"nrsc"
 )
 
 func main() {
 	nrsc.Handle("/static/")
+	nrsc.Mask(regexp.MustCompile(".gif$"))
 	http.HandleFunc("/", indexHandler)
 	if err := http.ListenAndServe(":%d", nil); err != nil {
 		fmt.Fprintf(os.Stderr, "error: %%s\n", err)