コード例 #1
0
ファイル: main_test.go プロジェクト: mehulsbhatt/pinghist
func Test_main_unit(t *testing.T) {
	Convey("main", t, func() {
		parts := strings.Split(time.Now().Format("2006-01-02-07:00"), "-")
		y, m, d, z := parts[0], parts[1], parts[2], parts[3]

		testTable := map[string]string{
			"01/01 01:52 pm": fmt.Sprintf("%s-01-01T13:52:00-%s", y, z),       // full
			"1/01 01:00 pm":  fmt.Sprintf("%s-01-01T13:00:00-%s", y, z),       // short day/month
			"01/1 01:00 pm":  fmt.Sprintf("%s-01-01T13:00:00-%s", y, z),       // short day/month
			"1/1 01:00 pm":   fmt.Sprintf("%s-01-01T13:00:00-%s", y, z),       // short day/month
			"01/01 1:00 pm":  fmt.Sprintf("%s-01-01T13:00:00-%s", y, z),       // short hour
			"1/01 1:00 pm":   fmt.Sprintf("%s-01-01T13:00:00-%s", y, z),       // short hour
			"01/1 1:00 pm":   fmt.Sprintf("%s-01-01T13:00:00-%s", y, z),       // short hour
			"1/1 1:00 pm":    fmt.Sprintf("%s-01-01T13:00:00-%s", y, z),       // short hour
			"01:52 pm":       fmt.Sprintf("%s-%s-%sT13:52:00-%s", y, m, d, z), // time
			"1:52 pm":        fmt.Sprintf("%s-%s-%sT13:52:00-%s", y, m, d, z), // time
			"01/01 13:52":    fmt.Sprintf("%s-01-01T13:52:00-%s", y, z),       // 24hr time
			"1/01 13:52":     fmt.Sprintf("%s-01-01T13:52:00-%s", y, z),       // 24hr time
			"01/1 13:52":     fmt.Sprintf("%s-01-01T13:52:00-%s", y, z),       // 24hr time
			"1/1 13:52":      fmt.Sprintf("%s-01-01T13:52:00-%s", y, z),       // 24hr time
			"13:52":          fmt.Sprintf("%s-%s-%sT13:52:00-%s", y, m, d, z), // 24hr time
		}

		for teststr, goodstr := range testTable {
			Convey("Given "+teststr, func() {
				Convey("time should equal "+goodstr, func() {
					t, err := ParseTime(teststr)
					So(err, ShouldBeNil)
					So(t.Format(time.RFC3339), ShouldEqual, goodstr)
				})
			})
		}
	})
}
コード例 #2
0
ファイル: profile_test.go プロジェクト: ChristopherRabotin/sg
func TestTokenized(t *testing.T) {
	Convey("Tokenizing data from a request works as expected", t, func() {
		// Let's setup a test server.
		var ts *httptest.Server
		ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			if r.Method == "GET" && r.URL.Path == "/test" {
				defer r.Body.Close()
				w.Header().Add("X-Custom-Hdr", "Custom Header")
				w.Header().Add("Set-Cookie", "session_id=42 ; Path=/")
				w.WriteHeader(200)
				fmt.Fprint(w, fmt.Sprintf(`{"URL": "%s", "json": true, "foolMeOnce": "shame on you"}`, r.URL))
			}
		}))
		Convey("Given a Tokenized objects, confirm that it formats the right information if does not format anything.", func() {
			t := Tokenized{Data: "test"}
			So(t.Format(nil), ShouldEqual, "test")
			So(t.String(), ShouldEqual, "{Tokenized with data}")
		})
		Convey("Given a Tokenized objects, confirm that it formats the right information if response is nil.", func() {
			t := Tokenized{Data: "test", Cookie: "ChocChip"}
			So(t.Format(nil), ShouldEqual, "test")
			So(t.String(), ShouldEqual, "{Tokenized with cookie with data}")
		})
		Convey("Given a Tokenized objects, confirm that it formats the right information.", func() {
			example := `<headers responseToken="resp" headerToken="hdr" cookieToken="cke">
							X-Fool:NotAMonkey resp/foolMeOnce
							Cookie:test=true;session_id=cke/session_id
							Some-Header:hdr/X-Custom-Hdr
							X-Cannot-Decode: resp/json
						</headers>`
			out := Tokenized{}
			xml.Unmarshal([]byte(example), &out)
			gresp, _ := goreq.Request{Uri: ts.URL + "/test"}.Do()
			resp := Response{}
			resp.FromGoResp(gresp, nil, time.Now())
			expectations := []string{"", "X-Fool:NotAMonkey shame on you", "Cookie:test=true;session_id=42", "Some-Header:Custom Header", "X-Cannot-Decode:", ""}
			for pos, line := range strings.Split(out.Format(&resp), "\n") {
				So(strings.TrimSpace(line), ShouldEqual, expectations[pos])
			}
			So(out.String(), ShouldEqual, "{Tokenized with cookie with header with data}")
		})
	})
}