Beispiel #1
0
func testResponses(t *testing.T, m *martini.ClassicMartini, expectations ...*Expectation) {
	for _, expectation := range expectations {
		req, err := http.NewRequest("GET", "/test", strings.NewReader(""))
		reflect.ValueOf(req).Elem().FieldByName("RemoteAddr").SetString("1.2.3.4:5000")

		if err != nil {
			t.Error(err)
		}

		time.Sleep(expectation.Wait)
		recorder := httptest.NewRecorder()
		m.ServeHTTP(recorder, req)
		expectStatusCode(t, expectation.StatusCode, recorder.Code)
		if expectation.Body != "" {
			expectSame(t, recorder.Body.String(), expectation.Body)
		}
		expectSame(t, recorder.Header()["X-Ratelimit-Limit"][0], expectation.RateLimitLimit)
		expectSame(t, recorder.Header()["X-Ratelimit-Remaining"][0], expectation.RateLimitRemaining)
		resetTime, err := strconv.ParseInt(recorder.Header()["X-Ratelimit-Reset"][0], 10, 64)
		if err != nil {
			t.Errorf(err.Error())
		}
		expectApproximateTimestamp(t, resetTime, expectation.RateLimitReset)
	}
}
func putStatement(t *testing.T, mart *martini.ClassicMartini, stmt, id string) {
	req, _ := http.NewRequest("PUT",
		"/test/test/statements?statementId="+id,
		strings.NewReader(stmt),
	)

	resp := httptest.NewRecorder()
	req.Header.Add("X-Experience-API-Version", "1.0.2")
	mart.ServeHTTP(resp, req)

	if got, expected := resp.Code, http.StatusNoContent; got != expected {
		t.Fatalf("Expected %v response code from put single statement; got %d", expected, got)
	}
}
Beispiel #3
0
func doRequest(m *martini.ClassicMartini, url string, method string, auth bool, body io.Reader) (*httptest.ResponseRecorder, *martini.ClassicMartini) {
	if m == nil {
		m = setup()
	}

	res := httptest.NewRecorder()
	req, _ := http.NewRequest(method, url, body)
	if auth {
		req.SetBasicAuth("default", "default")
	}

	m.ServeHTTP(res, req)

	return res, m
}
func getStatementWithHeader(t *testing.T, mart *martini.ClassicMartini, v *url.Values) ([]byte, http.Header) {
	resp := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "/test/test/statements?"+v.Encode(), nil)
	fatalIfError(t, err)

	req.Header.Add("X-Experience-API-Version", "1.0.2")
	mart.ServeHTTP(resp, req)

	if got, expected := resp.Code, http.StatusOK; got != expected {
		t.Fatalf("Expected %v response code from get statement(s); got %d", expected, got)
	}

	body, err := ioutil.ReadAll(resp.Body)
	fatalIfError(t, err)

	return body, resp.Header()
}
	AfterEach(func() {
		// Clear the database after each test.
		session.DB(dbName).DropDatabase()
	})

	Describe("GET /files", func() {

		// Set up a new GET request before every test
		// in this describe block.
		BeforeEach(func() {
			request, _ = http.NewRequest("GET", "/requestedFiles", nil)
		})

		Context("when no requestedFiles exist", func() {
			It("returns a status code of 200", func() {
				server.ServeHTTP(recorder, request)
				Expect(recorder.Code).To(Equal(200))
			})

			It("returns a empty body", func() {
				server.ServeHTTP(recorder, request)
				Expect(recorder.Body.String()).To(Equal("[]"))
			})
		})

		Context("when requestedFiles exist", func() {

			// Insert two valid requestedFiles into the database
			// before each test in this context.
			BeforeEach(func() {
				collection := session.DB(dbName).C("requestedFiles")
	AfterEach(func() {
		// Clear the database after each test.
		session.DB(dbName).DropDatabase()
	})

	Describe("GET /events", func() {

		// Set up a new GET request before every test
		// in this describe block.
		BeforeEach(func() {
			request, _ = http.NewRequest("GET", "/events", nil)
		})

		Context("when no events exist", func() {
			It("returns a status code of 200", func() {
				testServer.ServeHTTP(recorder, request)
				Expect(recorder.Code).To(Equal(200))
			})

			It("returns a empty body", func() {
				testServer.ServeHTTP(recorder, request)
				Expect(recorder.Body.String()).To(Equal("null"))
			})
		})

		Context("when events exist", func() {

			// Insert two valid signatures into the database
			// before each test in this context.
			BeforeEach(func() {
				collection := session.DB(dbName).C("events")
func postStatementWithFile(t *testing.T, mart *martini.ClassicMartini, stmt, id string) (contentSHA2sum string) {
	// construct content
	sha2 := sha256.New()
	content := bytes.NewBuffer(nil)

	// write content
	fmt.Fprintln(io.MultiWriter(content, sha2), "example content text")
	contentSHA2sum = fmt.Sprintf("%x", sha2.Sum(nil))

	// update statement
	var statement map[string]interface{}
	json.Unmarshal([]byte(stmt), &statement)
	statement["id"] = id
	statement["attachments"] = []map[string]interface{}{
		{
			"usageType": "http://example.com/attachment-usage/test",
			"display": map[string]interface{}{
				"en-US": "A test attachment",
			},
			"description": map[string]interface{}{
				"en-US": "A test attachment (description)",
			},
			"contentType": "text/plain; charset=ascii",
			"length":      content.Len(),
			"sha2":        contentSHA2sum,
		},
	}
	ustmt, _ := json.Marshal(statement)

	//
	// create multipart/form-data
	var header textproto.MIMEHeader
	buffer := bytes.NewBuffer(nil)
	encoder := multipart.NewWriter(buffer)

	// json field
	header = make(textproto.MIMEHeader)
	header.Add("Content-Type", "application/json")
	jsonfield, err := encoder.CreatePart(header)
	if err != nil {
		t.Fatal(err)
	}
	fmt.Fprintln(jsonfield, string(ustmt))

	// text (content) field
	header = make(textproto.MIMEHeader)
	header.Add("Content-Type", "text/plain")
	header.Add("Content-Transfer-Encoding", "binary")
	header.Add("X-Experience-API-Hash", contentSHA2sum)
	textfield, err := encoder.CreatePart(header)
	if err != nil {
		t.Fatal(err)
	}
	io.Copy(textfield, content)

	// finish writing
	encoder.Close()

	resp := httptest.NewRecorder()
	req, _ := http.NewRequest("POST", "/test/test/statements", buffer)
	req.Header.Add("Content-Type", "multipart/mixed; boundary="+encoder.Boundary())
	req.Header.Add("X-Experience-API-Version", "1.0.2")

	mart.ServeHTTP(resp, req)

	if got, expected := resp.Code, http.StatusOK; got != expected {
		r, _ := ioutil.ReadAll(resp.Body)
		t.Fatalf("Expected %v response code from post single statement with file; got %d, %v", expected, got, string(r))
	}

	return
}