package main import ( "flag" "fmt" "io/ioutil" "log" "math/rand" "net/http" "net/http/httptest" "net/url" "os" "strconv" "strings" "testing" "time" "github.com/dchest/captcha" clr "github.com/mewkiz/pkg/term" ) var () /* cosgo test suite: So far, testing for a few things: 1. POST URL Key MUST ALWAYS be served in the normal home page 2. POST URL Key MUST ALWAYS be present in user's POST message submissions 3. mbox gets bytes written to it on successful message send 4. Asset directories are restored (static, template) if they dont exist 5. Mbox is created on boot 6. CRONJOBs will not be broken, CLI Flags and Usage will not change for a while. TODO: 1. Config tests 2. Benchmarks 3. Stress test 4. Template fields testing */ var times []time.Duration var redwanted = clr.Red("Wanted:") var rednotfound = "" // grep is helper function to display the relative (target) line from a source string. func grep(source interface{}, target string) string { var str string switch source.(type) { case []byte: str = string(source.([]byte)) case string: str = source.(string) default: // working on it } defer func() { backup([]byte(str)) }() lines := strings.Split(str, "\n") for _, line := range lines { if strings.Contains(line, target) { return clr.MagentaBold("Found: ") + clr.MagentaBold(line) } } return "" } // In the case of a failed test, backup the response to /tmp/c_test_ThisTest_X func backup(b []byte) { tmpfile, _ := ioutil.TempFile(os.TempDir(), "c_test_"+strconv.Itoa(int(time.Now().Truncate(1*time.Minute).Unix()))) n, e := tmpfile.Write(b) if e != nil { panic(e) } fmt.Println("\t" + clr.Green(strconv.Itoa(n)+" bytes saved to "+tmpfile.Name())) } var originalFlags []string func init() { rand.NewSource(time.Now().UnixNano()) *quiet = true *mboxfile = "testing.mbox" originalFlags = os.Args } func randomDuration() (times []time.Duration) { t := uint64(rand.Uint32()*4) * uint64(rand.Uint32()) * uint64(rand.Uint32()*rand.Uint32()) / uint64(rand.Intn(10000)+1) times = append(times, time.Duration(t)) return times } func BenchmarkHumanize(b *testing.B) { for i := 0; i < 1000; i++ { b.StopTimer() rands := randomDuration() for _, t := range rands { b.StartTimer() humanize(t) b.StopTimer() } } } func TestHumanize(t *testing.T) { d := time.Duration(time.Hour * 24 * 7 * 4 * 8) s := humanize(d) if s != "8 months ago" { fmt.Println("\tGot:", s) fmt.Println("\t"+redwanted, "8 months ago") t.FailNow() } // Output: 8 months ago } // TestHomeHandler tests the home page, and makes sure the URLKey is in the output. func TestHomeHandler(t *testing.T) { c := setup() cwd, _ := os.Getwd() c.route(cwd) ts := httptest.NewServer(http.HandlerFunc(c.homeHandler)) defer ts.Close() time.Sleep(100 * time.Millisecond) key := c.URLKey res, err := http.Get(ts.URL) if err != nil { log.Fatal(err) } greeting, err := ioutil.ReadAll(res.Body) res.Body.Close() if err != nil { log.Fatal(err) } keyline := `
` if !strings.Contains(string(greeting), keyline) { fmt.Println("\t"+redwanted, keyline) fmt.Println("\t" + grep(greeting, `