Exemplo n.º 1
0
// GetJSResponse response to javascript request, based on the URL and the bookmarklet flag
func (controller *ShawtyJSController) GetJSResponse(url, creatorIP string, bm bool) (res *ResPkg) {
	domain := controller.config["SHAWTY_DOMAIN"]
	limit, _ := strconv.ParseUint(controller.config["SHAWTY_LPM"], 0, 32)

	res = NewResPkg()
	res.Data["Bookmarklet"] = bm
	res.Data["Domain"] = domain
	res.Data["Success"] = 1

	if !urlPattern.MatchString(url) {
		res.Data["Success"] = 0
		res.Errors = append(res.Errors, errors.New("a valid url has to start with http:// or https://"))
	} else {
		dupDomainPattern := regexp.MustCompile(fmt.Sprintf("^(?i)(https?)://%s.*", domain))
		if dupDomainPattern.MatchString(url) {
			res.Data["Success"] = 0
			res.Errors = append(res.Errors, errors.New("this url is already shortened"))
		} else {

			shawty, err := controller.sh.GetByUrl(url)

			if err != nil { // need to create
				minuteAgo := time.Unix(time.Now().Unix()-60, 0)
				numLinks, err := controller.sh.NumLinks(creatorIP, minuteAgo)

				if err != nil {
					res.Data["Success"] = 0
					res.Errors = append(res.Errors, errors.New("unknown error occured"))
					log.Errorf("%v", err)
				} else {

					// check for rate limit
					if numLinks >= uint32(limit) {
						res.Data["Success"] = 0
						res.Errors = append(res.Errors, errors.New("you are shortening your links too quickly"))
						log.Errorf("%s has reached the rate limit (%d)", creatorIP, limit)
					} else {
						shawty, err = controller.sh.Create("", url, creatorIP)
						if err != nil {
							res.Data["Success"] = 0
							res.Errors = append(res.Errors, errors.New("cannot shorten this link"))
							log.Errorf("%v", err)
						}
					}
				}
			}

			if res.Data["Success"] == 1 {
				res.Data["Shawty"] = shawty
				res.Data["Short"] = data.ShortID(shawty.ID, shawty.Rand)
			}
		}
	}

	return
}
Exemplo n.º 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)
	}
}
Exemplo n.º 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")
	}
}