Example #1
0
func TestObjectReadyMalformedUrl(t *testing.T) {
	dir, blobStore := setUpFakeBlobStore(t)
	defer os.RemoveAll(dir)

	cfg := &Environment{blobStore: blobStore}

	// TODO(mrjones): check error messages

	// No ".png" extension
	res := execute(t, "http://myhost.com/is_ready/100-1-2-3", IsReadyHandler, cfg)
	gt.AssertEqualM(t, http.StatusInternalServerError, res.StatusCode, "Should have been an error")

	// No "is_ready" path
	res = execute(t, "http://myhost.com/100-1-2-3.png", IsReadyHandler, cfg)
	gt.AssertEqualM(t, http.StatusInternalServerError, res.StatusCode, "Should have been an error")

	// Extraneous path
	res = execute(t, "http://myhost.com/random/is_ready/100-1-2-3.png", IsReadyHandler, cfg)
	gt.AssertEqualM(t, http.StatusInternalServerError, res.StatusCode, "Should have been an error")

	// Not enough parts in the handle
	res = execute(t, "http://myhost.com/is_ready/100-1-2.png", IsReadyHandler, cfg)
	gt.AssertEqualM(t, http.StatusInternalServerError, res.StatusCode, "Should have been an error")

	// Non-numeric parts in the handle
	res = execute(t, "http://myhost.com/is_ready/a-1-2-3.png", IsReadyHandler, cfg)
	gt.AssertEqualM(t, http.StatusInternalServerError, res.StatusCode, "Should have been an error")
}
Example #2
0
func TestAsyncTaskCreation(t *testing.T) {
	q := &MockTaskQueue{}
	cfg := &Environment{taskQueue: q}
	s := "lllat=1.0&lllng=2.0&urlat%3d3.0&urlng=4.0&start=5&end=6"
	u := "http://myhost.com/async_drawmap/?code=vercode&state=" + url.QueryEscape(s)

	res := execute(t, u, AsyncDrawMapHandler, cfg)

	gt.AssertEqualM(t, http.StatusFound, res.StatusCode,
		"Should redirect. Body: "+res.Body)
	// TODO(mrjones): verify URL better.
	//	gt.AssertEqualM(t, "/display/100-1-2-3.png", res.Headers.Get("Location"),
	//		"Should redirect to specified URL")

	gt.AssertEqualM(t, "/drawmap_worker", q.lastUrl, "Should enqueue a drawmap worker")
	rawS := q.lastParams.Get("state")
	rawS, err := url.QueryUnescape(rawS)
	gt.AssertNil(t, err)
	parsedS, err := url.ParseQuery(rawS)

	gt.AssertEqualM(t, "1.0000000000000000", parsedS.Get("lllat"), "token")
	gt.AssertEqualM(t, "2.0000000000000000", parsedS.Get("lllng"), "token")
	gt.AssertEqualM(t, "3.0000000000000000", parsedS.Get("urlat"), "token")
	gt.AssertEqualM(t, "4.0000000000000000", parsedS.Get("urlng"), "token")
	gt.AssertEqualM(t, "5", parsedS.Get("start"), "token")
	gt.AssertEqualM(t, "6", parsedS.Get("end"), "token")
	gt.AssertEqualM(t, "vercode", q.lastParams.Get("verification_code"), "code")
	//	gt.AssertEqualM(t, "abc", q.lastParams.Get("access_token"), "token")
	//	gt.AssertEqualM(t, "def", q.lastParams.Get("refresh_token"), "token")
}
Example #3
0
func TestSquareBox(t *testing.T) {
	box, err := NewBoundingBox(
		Coordinate{Lat: 0, Lng: 0},
		Coordinate{Lat: 10, Lng: 10})
	gt.AssertNil(t, err)

	w, h := imgSize(box, 500)

	gt.AssertEqualM(t, 500, w, "Width should be maxed for a square box")
	gt.AssertEqualM(t, 500, h, "Height should be maxed for a square box")
}
Example #4
0
func TestTallBox(t *testing.T) {
	box, err := NewBoundingBox(
		Coordinate{Lat: 0, Lng: 0},
		Coordinate{Lat: 10, Lng: 5})
	gt.AssertNil(t, err)

	w, h := imgSize(box, 500)

	gt.AssertEqualM(t, 250, w, "Width should be narrow for a tall box")
	gt.AssertEqualM(t, 500, h, "Height should be maxed for a tall box")
}
Example #5
0
func TestHandleUrlString(t *testing.T) {
	h := simpleHandle()

	gt.AssertEqualM(t,
		"/page/0-1-2-3.xyz",
		serializeHandleToUrl(h, "xyz", "page"),
		"Unexpected serialization")
}
Example #6
0
func TestSuccessfulUrlSerializesAndDeserialize(t *testing.T) {
	h := simpleHandle()

	url := serializeHandleToUrl(h, "suffix", "page")
	h2, err := parseHandleFromUrl(url)

	gt.AssertNil(t, err)
	gt.AssertEqualM(t, h, h2, "Expected serialize/deserialize to return the same result.")
}
Example #7
0
func TestObjectReady(t *testing.T) {
	dir, blobStore := setUpFakeBlobStore(t)
	defer os.RemoveAll(dir)

	cfg := NewEnvironment(blobStore, nil, nil, nil)

	res1 := execute(t, "http://myhost.com/is_ready/100-1-2-3.png", IsReadyHandler, cfg)
	gt.AssertEqualM(t, http.StatusOK, res1.StatusCode, "Request should have succeeded")
	gt.AssertEqualM(t, "fail", res1.Body, "Should not have found the object")

	h := &Handle{n1: 1, n2: 2, n3: 3, timestamp: 100}
	err := blobStore.Store(h, &Blob{})
	gt.AssertNil(t, err)

	res2 := execute(t, "http://myhost.com/is_ready/100-1-2-3.png", IsReadyHandler, cfg)
	gt.AssertEqualM(t, http.StatusOK, res2.StatusCode, "Request should have succeeded")
	gt.AssertEqualM(t, "ok", res2.Body, "Should have found the object this time.")
}
Example #8
0
func TestDisplayPage(t *testing.T) {
	cfg := &Environment{}

	u := "http://myhost.com/display/100-1-2-3.png"
	res := execute(t, u, ResultPageHandler, cfg)

	gt.AssertEqualM(t, http.StatusOK, res.StatusCode, "")
	gt.AssertTrueM(t, strings.Contains(res.Body, "loadImage('100-1-2-3.png'"),
		"Missing expected loadImage call in ["+res.Body+"]")
}
Example #9
0
func TestSuccessfulParamsSerializeAndDeserialize(t *testing.T) {
	h := simpleHandle()

	var p = make(url.Values)

	serializeHandleToParams(h, &p)
	h2, err := parseHandleFromParams(&p)

	gt.AssertNil(t, err)
	gt.AssertEqualM(t, h, h2, "Should be equal")
}
Example #10
0
func assertImage(t *testing.T, expected [][]color.Color, actual image.Image) {
	gt.AssertEqualM(t, len(expected), actual.Bounds().Dy(), "Unexpected image height")
	gt.AssertEqualM(t, len(expected[0]), actual.Bounds().Dx(), "Unexpected image width")

	// WxH image (0,0) is in the top left:
	// (i.e. increasing Y-coordinates move downwards)
	// (0,0) ----- (W,0)
	//   |           |
	// (0,H) ----- (W,H)
	//
	// This works, because the ASCII arrays in the asserts are laid out similarly.
	// However, it means that printed coordinates might be y-inverted from
	// intuition.
	for x := 0; x < len(expected[0]); x++ {
		for y := 0; y < len(expected); y++ {
			expPix := expected[y][x]
			gt.AssertEqualM(t, expPix, actual.At(x, y),
				fmt.Sprintf("Unexpected pixel at: (%d, %d)", x, y))
		}
	}
}
Example #11
0
func TestHandleDebugString(t *testing.T) {
	h := simpleHandle()

	gt.AssertEqualM(t, "0-123", h.String(), "Unexpected handle")
}
Example #12
0
func TestAsyncWorker(t *testing.T) {
	mockEngine := &MockRenderEngine{}
	cfg := &Environment{mockRenderEngine: mockEngine}

	s := "lllat=1.0&lllng=2.0&urlat=3.0&urlng=4.0&start=5&end=6"
	u := "http://myhost.com/drawmap_worker/?state=" + url.QueryEscape(s) + "&access_token=abc&refresh_token=def&expiration_time=1234567890&hStamp=100&h1=1&h2=2&h3=3"

	res := execute(t, u, DrawMapWorker, cfg)

	if mockEngine.lastRenderRequest == nil {
		t.Fatal("No render request was made!")
	}
	gt.AssertEqualM(t, 1.0, mockEngine.lastRenderRequest.bounds.LowerLeft().Lat, "")
	gt.AssertEqualM(t, 2.0, mockEngine.lastRenderRequest.bounds.LowerLeft().Lng, "")
	gt.AssertEqualM(t, 3.0, mockEngine.lastRenderRequest.bounds.UpperRight().Lat, "")
	gt.AssertEqualM(t, 4.0, mockEngine.lastRenderRequest.bounds.UpperRight().Lng, "")

	gt.AssertEqualM(t, time.Unix(5, 0).UTC(), mockEngine.lastRenderRequest.start, "")
	gt.AssertEqualM(t, time.Unix(6, 0).UTC(), mockEngine.lastRenderRequest.end, "")

	gt.AssertEqualM(t, int64(100), mockEngine.lastHandle.timestamp, "")
	gt.AssertEqualM(t, int64(1), mockEngine.lastHandle.n1, "")
	gt.AssertEqualM(t, int64(2), mockEngine.lastHandle.n2, "")
	gt.AssertEqualM(t, int64(3), mockEngine.lastHandle.n3, "")

	gt.AssertEqualM(t, http.StatusOK, res.StatusCode, "")
}