func (sh *MySh) Create(r string, url, creatorIP string) (*Shawty, error) { if r == "" { r = utils.ToSafeBase(uint64(sh.random.Byte()) % utils.BaseLen) } var stmt, err = sh.db.Prepare("insert ignore into `shawties`(`Rand`, `Hits`, `Url`, `CreatorIP`, `CreatedOn`) values(?, 0, ?, ?, ?)") if err != nil { return nil, err } defer stmt.Close() var now = time.Now() result, err := stmt.Exec(r, url, creatorIP, now.Unix()) if err != nil { return nil, err } id, err := result.LastInsertId() if err != nil { return nil, err } var s = &Shawty{ ID: uint64(id), Rand: r, Hits: 0, Url: url, CreatorIP: creatorIP, CreatedOn: now} log.Infof("Created link ID %d", id) return s, nil }
func testShawtyJSValidUrl(t *testing.T, url string, expectedID uint64) { conf, seed, sh := getShawtyJSTestData() controller := NewShawtyJSController(conf, sh) res := controller.GetJSResponse(url, "127.0.0.1", false) shortID := data.ShortID(expectedID, utils.ToSafeBase(seed)) if res == nil { t.Error("No response") t.FailNow() } if res.Data["Success"] != 1 { t.Errorf("'Success' flag in the data needs to be '1', but got %v instead", res.Data["Success"]) } if res.Data["Domain"] != conf["SHAWTY_DOMAIN"] { t.Errorf( "Wrong 'Domain' returned. '%s' expected, but '%s' returned instead", conf["SHAWTY_DOMAIN"], res.Data["Domain"]) } if res.Data["Short"] != shortID { t.Errorf("Data[Short] expected to be %s, but %s returned", shortID, res.Data["Short"]) } shawty, _ := sh.GetByUrl(url) if res.Data["Shawty"].(*data.Shawty).ID != shawty.ID { t.Errorf("Wrong 'Shawty' returned. Expecting %v, got %v", shawty, res.Data["Shawty"].(*data.Shawty)) } }
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 }
// 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) } }
// 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") } }
// ShortID constructs a short ID func ShortID(id uint64, r string) string { return r + utils.ToSafeBase(id) }