コード例 #1
0
ファイル: build_test.go プロジェクト: leighmcculloch/static
func TestBuildSingleErrorsCreatingPath(t *testing.T) {
	t.Log("When a Handler is defined to respond to /* and response with Hello <path>!")
	handler := http.NewServeMux()
	handler.HandleFunc("/hello/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello %s!", filepath.Base(r.URL.Path))
	})

	t.Log("And Options are defined with defaults and an OutputDir.")
	options := static.DefaultOptions
	tempDir, _ := ioutil.TempDir("", "")
	options.OutputDir = filepath.Join(tempDir, "build")
	t.Logf("OutputDir => %s", options.OutputDir)

	t.Log("And a file exists at the same path as the OutputDir (a problem).")
	f, _ := os.Create(options.OutputDir)
	defer f.Close()

	t.Log("And the path to build is /hello/world.")
	path := "/hello/world"

	t.Log("Expect BuildSingle to error with not an unable to create dir error.")

	expectedStatus := 0
	expectedErrString := "Unable to create dir"
	status, outputPath, err := static.BuildSingle(options, handler, path)
	if err != nil && strings.Contains(err.Error(), expectedErrString) {
		t.Logf("BuildSingle(%#v) => %v, %v, %v", path, status, outputPath, err)
	} else {
		t.Errorf("BuildSingle(%#v) => %v, %v, %v, expected %v, nil and a %s error", path, status, outputPath, err, expectedStatus, expectedErrString)
	}
}
コード例 #2
0
ファイル: example_test.go プロジェクト: leighmcculloch/static
func ExampleBuildSingle() {
	handler := http.NewServeMux()

	handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello %s!", path.Base(r.URL.Path))
	})

	statusCode, outputPath, err := static.BuildSingle(static.DefaultOptions, handler, "/world")
	fmt.Printf("Built: /world, StatusCode: %d, OutputPath: %v, Error: %v", statusCode, outputPath, err)

	// Output:
	// Built: /world, StatusCode: 200, OutputPath: build/world, Error: <nil>
}
コード例 #3
0
ファイル: build_test.go プロジェクト: leighmcculloch/static
func TestBuildSingleDirFilename(t *testing.T) {
	t.Log("When a Handler is defined to respond to /* and responds with Hello directory!")
	handler := http.NewServeMux()
	handler.HandleFunc("/hello/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Hello directory!")
	})

	t.Log("And Options are defined with defaults and an OutputDir that does not exist.")
	options := static.DefaultOptions
	tempDir, _ := ioutil.TempDir("", "")
	options.OutputDir = filepath.Join(tempDir, "build")
	t.Logf("OutputDir => %s", options.OutputDir)

	t.Log("And Options are defined with the default DirFilename of index.html.")

	t.Log("And the path to build is the directory path /hello/.")
	path := "/hello/"

	t.Log("Expect BuildSingle to create the default DirFilename at the output path with contents Hello directory! And return no error.")
	expectedStatus := 200
	expectedOutputFilePath := filepath.Join(options.OutputDir, "hello", "index.html")
	expectedOutputFileContents := "Hello directory!"

	status, outputPath, err := static.BuildSingle(options, handler, path)
	t.Logf("BuildSingle(%#v) => %v, %v, %v", path, status, outputPath, err)
	if (status != expectedStatus && outputPath != expectedOutputFilePath) || err != nil {
		t.Errorf("BuildSingle(%#v) => %v, %v, %v, expected %v, %v, nil", path, status, outputPath, outputPath, err, expectedStatus)
	}

	outputFileContents, err := ioutil.ReadFile(expectedOutputFilePath)
	if err != nil {
		t.Fatalf("Expected %s to exist with the output but got error when opening: %v", expectedOutputFilePath, err)
	}

	t.Logf("Contents of %s => %s", expectedOutputFilePath, outputFileContents)
	if string(outputFileContents) != expectedOutputFileContents {
		t.Fatalf(`Contents of %s => %s, expected %s`, expectedOutputFilePath, outputFileContents, expectedOutputFileContents)
	}
}