Exemple #1
0
func TestIDEncoder(t *testing.T) {
	is := is.New(t)
	e := asset.NewIDEncoder()
	is.OK(e)
	_, err := io.Copy(e, strings.NewReader(`This is a pretend asset file, for testing asset id generation.
`))
	is.NoErr(err)

	id := e.ID()
	is.OK(id)
	is.Equal(id.String(), `c43UBJqUTjQyrcRv43pgt1UWqysgNud7a7Kohjp1Z4w1gD8LGv4p1FK48kC8ufPPRpbEtc8inVhxuFQ453GcfRFE9d`)
	// Added test for mutability bug. Calling String() should not alter id!
	is.Equal(id.String(), `c43UBJqUTjQyrcRv43pgt1UWqysgNud7a7Kohjp1Z4w1gD8LGv4p1FK48kC8ufPPRpbEtc8inVhxuFQ453GcfRFE9d`)
}
Exemple #2
0
func TestEncoder(t *testing.T) {
	is := is.New(t)

	options := &respond.Options{}
	var encodercall map[string]interface{}
	options.Encoder = func(w http.ResponseWriter, r *http.Request) respond.Encoder {
		encodercall = map[string]interface{}{
			"w": w, "r": r,
		}
		return &testEncoder{}
	}

	testHandler := &testHandler{
		status: http.StatusOK, data: testdata,
	}
	handler := options.Handler(testHandler)

	w := httptest.NewRecorder()
	r := newTestRequest()

	handler.ServeHTTP(w, r)

	is.OK(encodercall)
	is.Equal(encodercall["w"], w)
	is.Equal(encodercall["r"], r)

	is.Equal(http.StatusOK, w.Code)
	is.Equal(w.Body.String(), "testEncoder")
	is.Equal(w.HeaderMap.Get("Content-Type"), "test/encoder")

}
Exemple #3
0
func TestEncoderOnErr(t *testing.T) {
	is := is.New(t)

	var onErrCall map[string]interface{}
	options := &respond.Options{
		OnErr: func(err error) {
			onErrCall = map[string]interface{}{"err": err}
		},
	}
	encoderErr := errors.New("something went wrong while encoding")
	options.Encoder = func(w http.ResponseWriter, r *http.Request) respond.Encoder {
		return &testEncoder{
			err: encoderErr,
		}
	}

	testHandler := &testHandler{
		status: http.StatusOK, data: testdata,
	}
	handler := options.Handler(testHandler)

	w := httptest.NewRecorder()
	r := newTestRequest()

	handler.ServeHTTP(w, r)

	is.OK(onErrCall)
	is.Equal(onErrCall["err"], encoderErr)

}
Exemple #4
0
func TestIDofIDSlice(t *testing.T) {
	is := is.New(t)
	var b, s []byte
	for i := 0; i < 64; i++ {
		b = append(b, 0xFF)
		s = append(s, 0x00)
	}
	bigBig := big.NewInt(0)
	bigSmall := big.NewInt(0)
	bigBig = bigBig.SetBytes(b)
	bigSmall = bigSmall.SetBytes(s)
	bigID := asset.ID(*bigBig)
	smallID := asset.ID(*bigSmall)

	encoder := asset.NewIDEncoder()
	is.OK(encoder)
	_, err := io.Copy(encoder, strings.NewReader(`c41111111111111111111111111111111111111111111111111111111111111111111111111111111111111111c467RPWkcUr5dga8jgywjSup7CMoA9FNqkNjEFgAkEpF9vNktFnx77e2Js11EDL3BNu9MaKFUbacZRt1HYym4b8RNp`))
	is.NoErr(err)
	id := encoder.ID()

	var idSlice asset.IDSlice
	idSlice.Push(&bigID)
	idSlice.Push(&smallID)
	sliceID, err := idSlice.ID()
	is.NoErr(err)

	is.Equal(sliceID.String(), id.String())
}
Exemple #5
0
func TestLogin(t *testing.T) {
	is := is.New(t)
	userRepositoryMock := &UserRepositoryMock{}
	userRepositoryMock.CheckCredentialsCall.Returns.UserType = "freelancer"
	userRepositoryMock.CheckCredentialsCall.Returns.User = User{
		Model: Model{
			ID: 1,
		},
		FirstName: "firstname",
		LastName:  "lastname",
		Password:  "******",
		Email:     "*****@*****.**",
	}
	userContext := &ApplicationContext{
		UserRepository: userRepositoryMock,
	}

	r := getRequest(userContext, `
		{
			"email": "*****@*****.**",
			"password": "******"
		}
	`)
	w := httptest.NewRecorder()

	Login(w, r)

	is.Equal(w.Code, http.StatusOK)
	var body map[string]interface{}
	is.NoErr(json.Unmarshal(w.Body.Bytes(), &body))
	is.Equal(body["id"], 1)
	is.OK(body["token"])
	is.Equal(body["type"], "freelancer")
}
Exemple #6
0
func TestAfter(t *testing.T) {
	is := is.New(t)

	options := &respond.Options{}
	var aftercall map[string]interface{}
	options.After = func(w http.ResponseWriter, r *http.Request, status int, data interface{}) {
		aftercall = map[string]interface{}{
			"w": w, "r": r, "status": status, "data": data,
		}
	}

	testHandler := &testHandler{
		status: http.StatusOK, data: testdata,
	}
	handler := options.Handler(testHandler)

	w := httptest.NewRecorder()
	r := newTestRequest()

	handler.ServeHTTP(w, r)

	is.OK(aftercall)
	is.Equal(aftercall["w"], w)
	is.Equal(aftercall["r"], r)
	is.Equal(aftercall["status"], testHandler.status)
	is.Equal(aftercall["data"], testHandler.data)

	is.Equal(http.StatusOK, w.Code)
	var data map[string]interface{}
	is.NoErr(json.Unmarshal(w.Body.Bytes(), &data))
	is.Equal(data, testdata)
	is.Equal(w.HeaderMap.Get("Content-Type"), "application/json; charset=utf-8")

}
Exemple #7
0
func TestRetryLimit(t *testing.T) {
	is := is.New(t)
	err := try.Do(func(attempt int) (bool, error) {
		return true, errors.New("nope")
	})
	is.OK(err)
	is.Equal(try.IsMaxRetries(err), true)
}
Exemple #8
0
func TestTime(t *testing.T) {
	is := is.New(t)
	timestr := "02 Jan 06 15:04 MST"
	timeParser := parsers.Time(time.RFC822)
	parser := append(DefaultParser, timeParser)
	val, fn := parser.parseWith(timestr)
	is.OK(fn)
	is.Equal(val.(time.Time).Format(time.RFC822), timestr)
	is.Equal(fmt.Sprint(fn), fmt.Sprint(timeParser))
}
Exemple #9
0
func TestEmail(t *testing.T) {
	is := is.New(t)
	src := []byte("Send me an email to [email protected] if you like.")
	matches, err := anno.Emails(src)
	is.NoErr(err)
	is.OK(matches)
	is.Equal(len(matches), 1)
	is.Equal(matches[0].Val, []byte("*****@*****.**"))
	is.Equal(matches[0].Start, 20)
	is.Equal(matches[0].End(), 20+len(matches[0].Val))
	is.Equal(matches[0].Kind, "email")
}
Exemple #10
0
func TestJvFromJSONString(t *testing.T) {
	is := is.New(t)

	jv, err := jq.JvFromJSONString("[]")
	is.NoErr(err)
	is.OK(jv)
	is.Equal(jv.Kind(), jq.JV_KIND_ARRAY)

	jv, err = jq.JvFromJSONString("not valid")
	is.Err(err)
	is.Nil(jv)
}
Exemple #11
0
func TestLineComments(t *testing.T) {
	is := is.New(t)
	l, err := parse.ParseLine(0, []byte(`* Key: "Value" // comments should be ignored`))
	is.NoErr(err)
	detail := l.Detail()
	is.OK(detail)
	is.Equal(detail.Key, "Key")
	is.Equal(detail.Value.Data, "Value")
	l, err = parse.ParseLine(0, []byte(`* Key: "Value" // comments should be ignored`))
	is.NoErr(err)
	is.Equal(string(l.Bytes), `* Key: "Value"`)
}
Exemple #12
0
func TestLineParams(t *testing.T) {
	is := is.New(t)
	for i, line := range []string{
		"* ?key=value",
		"* `?key=value`",
		"* ?`key`=`value`",
	} {
		l, err := parse.ParseLine(i, []byte(line))
		is.NoErr(err)
		is.Equal(l.Type, parse.LineTypeParam)
		detail := l.Detail()
		is.OK(detail)
		is.Equal(detail.Key, "key")
		is.Equal(detail.Value.Data, "value")
	}
}
Exemple #13
0
func TestMention(t *testing.T) {
	is := is.New(t)
	src := []byte("Call me @matryer on Twitter, or follow @downlistapp instead.")
	matches, err := anno.Mentions(src)
	is.NoErr(err)
	is.OK(matches)
	is.Equal(len(matches), 2)
	is.Equal(matches[0].Val, []byte("@matryer"))
	is.Equal(matches[0].Start, 8)
	is.Equal(matches[0].End(), 8+len(matches[0].Val))
	is.Equal(matches[0].Kind, "mention")
	is.Equal(matches[1].Val, []byte("@downlistapp"))
	is.Equal(matches[1].Start, 39)
	is.Equal(matches[1].End(), 39+len(matches[1].Val))
	is.Equal(matches[1].Kind, "mention")
}
Exemple #14
0
func TestHashtag(t *testing.T) {
	is := is.New(t)
	src := []byte("I love programming in #golang - it's #lovely.")
	matches, err := anno.Hashtags(src)
	is.NoErr(err)
	is.OK(matches)
	is.Equal(len(matches), 2)
	is.Equal(matches[0].Val, []byte("#golang"))
	is.Equal(matches[0].Start, 22)
	is.Equal(matches[0].End(), 22+len(matches[0].Val))
	is.Equal(matches[0].Kind, "hashtag")
	is.Equal(matches[1].Val, []byte("#lovely"))
	is.Equal(matches[1].Start, 37)
	is.Equal(matches[1].End(), 37+len(matches[1].Val))
	is.Equal(matches[1].Kind, "hashtag")
}
Exemple #15
0
func TestURL(t *testing.T) {
	is := is.New(t)
	src := []byte("My website is https://downlist.io/ come and check it out - or go to http://www.codeandthat.com/ instead.")
	matches, err := anno.URLs(src)
	is.NoErr(err)
	is.OK(matches)
	is.Equal(len(matches), 2)
	is.Equal(matches[0].Val, []byte("https://downlist.io/"))
	is.Equal(matches[0].Start, 14)
	is.Equal(matches[0].End(), 14+len(matches[0].Val))
	is.Equal(matches[0].Kind, "url")
	is.Equal(matches[1].Val, []byte("http://www.codeandthat.com/"))
	is.Equal(matches[1].Start, 68)
	is.Equal(matches[1].End(), 68+len(matches[1].Val))
	is.Equal(matches[1].Kind, "url")
}
Exemple #16
0
func TestIDEncoderReset(t *testing.T) {
	is := is.New(t)
	e := asset.NewIDEncoder()
	is.OK(e)
	for i := 0; i < 10; i++ {
		s := strconv.Itoa(i)
		e2 := asset.NewIDEncoder()
		_, err := io.Copy(e, strings.NewReader(s))
		is.NoErr(err)
		_, err2 := io.Copy(e2, strings.NewReader(s))
		is.NoErr(err2)
		id1 := e.ID()
		id2 := e2.ID()
		is.Equal(id1, id2)
		e.Reset()
	}
}
Exemple #17
0
func TestEmojiWithPunctuation(t *testing.T) {
	is := is.New(t)
	src := []byte(`":beer:"?`)
	notes, err := emoji.Find(src)
	is.NoErr(err)
	is.OK(notes)
	is.Equal(len(notes), 1)
	is.Equal(notes[0].Val, []byte(`:beer:`))
	is.Equal(notes[0].Start, 1)
	is.Equal(notes[0].End(), 1+len(notes[0].Val))
	is.Equal(notes[0].Kind, "emoji")

	e := anno.Expander{
		"emoji": emoji.Expand,
	}
	is.Equal(e.Expand(string(src), notes), `"🍺"?`)

}
Exemple #18
0
func TestLineComments(t *testing.T) {
	is := is.New(t)
	l, err := parse.ParseLine(0, []byte(`* Key: "Value" // comments should be ignored`))
	is.NoErr(err)
	is.Equal(l.Comment, " comments should be ignored")
	detail := l.Detail()
	is.OK(detail)
	is.Equal(detail.Key, "Key")
	is.Equal(detail.Value.Data, "Value")
	l, err = parse.ParseLine(0, []byte(`* Key: "Value" // comments should be ignored`))
	is.NoErr(err)
	is.Equal(string(l.Bytes), `* Key: "Value"`)

	// if the comment contains a {placeholder}, grab it
	l, err = parse.ParseLine(0, []byte(`* Key: "Value" // will be stored as {id}`))
	is.NoErr(err)
	is.Equal(l.Comment, " will be stored as {id}")
	is.Equal(l.Capture(), "id")
}
Exemple #19
0
func TestTemple(t *testing.T) {
	is := is.New(t)

	tpl, err := New("test")
	is.NoErr(err)
	is.OK(tpl)
	log.Println(tpl.templates)
	_, ok := tpl.GetOK("site.welcome.about.nested")
	is.True(ok)

	data := map[string]interface{}{"Name": "Mat"}
	var buf bytes.Buffer
	is.NoErr(tpl.Get("site.welcome.about.nested").Execute(&buf, data))
	is.Equal(buf.String(), `<base>Hello Mat.</base>`)

	buf.Reset()
	is.NoErr(tpl.Get("site.welcome").Execute(&buf, data))
	is.Equal(buf.String(), `<base>Welcome</base>`)

}
Exemple #20
0
func TestEmoji(t *testing.T) {
	is := is.New(t)
	src := []byte(":beer: makes me want to :smile: you know.")
	notes, err := emoji.Find(src)
	is.NoErr(err)
	is.OK(notes)
	is.Equal(len(notes), 2)
	is.Equal(notes[0].Val, []byte(":beer:"))
	is.Equal(notes[0].Start, 0)
	is.Equal(notes[0].End(), 0+len(notes[0].Val))
	is.Equal(notes[0].Kind, "emoji")
	is.Equal(notes[1].Val, []byte(":smile:"))
	is.Equal(notes[1].Start, 24)
	is.Equal(notes[1].End(), 24+len(notes[1].Val))
	is.Equal(notes[1].Kind, "emoji")

	e := anno.Expander{
		"emoji": emoji.Expand,
	}
	is.Equal(e.Expand(string(src), notes), "🍺 makes me want to 😄 you know.")

}