Пример #1
0
func (ms *MemSh) Create(r, url, creatorIP string) (*Shawty, error) {
	if r == "" {
		r = utils.ToSafeBase(uint64(ms.random.Byte()) % utils.BaseLen)
	}
	sh := &Shawty{
		ID:        ms.nextID,
		Rand:      r,
		Hits:      0,
		Url:       url,
		CreatorIP: creatorIP,
		CreatedOn: time.Now()}
	ms.data = append(ms.data, sh)
	ms.nextID++
	return sh, nil
}
Пример #2
0
// TestShortIDNotFound tests the response when a short ID is requested which is in the data store
func TestShortIDNotFound(t *testing.T) {
	conf, _, sh := getShortIDTestData()
	controller := NewShortIDController(conf, sh)

	shortID := data.ShortID(5, utils.ToSafeBase(1))
	res := controller.Respond(shortID)

	if res == nil {
		t.Error("No response")
		t.FailNow()
	}

	if res.HttpStatus != http.StatusNotFound {
		t.Errorf(
			"HTTP status needs to be %d when Shawty found, but %d returned instead",
			http.NotFound, res.HttpStatus)
	}
}
Пример #3
0
// TestShortIDFound tests the response when a short ID is requested which is in the data store
func TestShortIDFound(t *testing.T) {
	conf, seed, sh := getShortIDTestData()
	controller := NewShortIDController(conf, sh)

	shortID := data.ShortID(2, utils.ToSafeBase(seed))
	res := controller.Respond(shortID)

	if res == nil {
		t.Error("No response")
		t.FailNow()
	}

	if res.HttpStatus != http.StatusMovedPermanently {
		t.Errorf(
			"HTTP status needs to be %d when Shawty found, but %d returned instead",
			http.StatusMovedPermanently, res.HttpStatus)
	}

	if res.Data["Domain"] != conf["SHAWTY_DOMAIN"] {
		t.Errorf(
			"Wrong 'Domain' returned. '%s' expected, but '%s' returned instead",
			conf["SHAWTY_DOMAIN"], res.Data["Domain"])
	}

	shawty2, _ := sh.GetByUrl("http://test.com/url2")

	if res.Data["Shawty"].(*data.Shawty).ID != shawty2.ID {
		t.Errorf("Wrong 'Shawty' returned. Expected %v, but %v returned", shawty2, res.Data["Shawty"])
		t.FailNow()
	}

	// make sure it increased hits
	if shawty2.Hits != 1 {
		t.Error("Respond is expected to increase hits when the requested Shawty is found")
	}
}