func initHandler(s *mgo.Session) *martini.ClassicMartini {
	mart := martini.Classic()
	hand := New(s)
	mart.Get("/:user/:app/statements", acceptlang.Languages(), hand.FindStatement)
	mart.Put("/:user/:app/statements", hand.StoreStatement)
	mart.Post("/:user/:app/statements", hand.StoreMultStatement)

	return mart
}
func TestPutStatementAndCheckAuthority(t *testing.T) {
	m := martini.Classic()

	sess, err := mgo.Dial(miscs.GlobalConfig.MongoDB.URL)
	defer sess.Close()
	if err != nil {
		t.Fatalf("%v", err)
	}
	c := New(sess)

	m.Get("/:user/:app/statements", acceptlang.Languages(), c.FindStatement)
	m.Put("/:user/:app/statements", c.StoreStatement)
	stmt := singleStatement01

	resp := httptest.NewRecorder()
	statementID := uuid.NewV4().String()
	req, _ := http.NewRequest("PUT",
		"/test/test/statements?statementId="+statementID,
		strings.NewReader(stmt),
	)
	req.Header.Add("X-Experience-API-Version", "1.0.2")
	req.Header.Add("X-Edo-Ta-Id", "example-ta-id")
	req.Header.Add("X-Edo-User-Id", "example-user-id")

	m.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)
	}

	resp2 := httptest.NewRecorder()
	req2, _ := http.NewRequest("GET", "/test/test/statements?statementId="+statementID, nil)
	req2.Header.Add("X-Experience-API-Version", "1.0.2")

	m.ServeHTTP(resp2, req2)

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

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

	rstmt, err := gabs.ParseJSON(body)
	fatalIfError(t, err)

	v, ok := rstmt.Path("authority.account.homePage").Data().(string)
	if !ok || v != "example-ta-id" {
		t.Fatal("Field authority.account.homePage is invalid or not found in get response")
	}
	v, ok = rstmt.Path("authority.account.name").Data().(string)
	if !ok || v != "example-user-id" {
		t.Fatal("Field authority.account.name is invalid or not found in get response")
	}
}
func TestPostAndGetStatement(t *testing.T) {
	m := martini.Classic()

	sess, err := mgo.Dial(miscs.GlobalConfig.MongoDB.URL)
	defer sess.Close()
	if err != nil {
		t.Fatalf("%v", err)
	}
	c := New(sess)

	m.Get("/:user/:app/statements", acceptlang.Languages(), c.FindStatement)
	m.Post("/:user/:app/statements", c.StoreMultStatement)

	for _, stmt := range postStatementTestCases {
		resp := httptest.NewRecorder()
		req, _ := http.NewRequest("POST", "/test/test/statements", strings.NewReader(stmt))
		req.Header.Add("X-Experience-API-Version", "1.0.2")

		m.ServeHTTP(resp, req)

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

		var statementIDs []interface{}

		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			t.Fatalf("A POST request with statement(s) returns empty body.")
		}

		err = json.Unmarshal(body, &statementIDs)
		if err != nil {
			t.Fatalf("A POST request with statement(s) returns non-json string.")
		}

		for _, v := range statementIDs {
			id := v.(string)

			resp2 := httptest.NewRecorder()
			req2, _ := http.NewRequest("GET", "/test/test/statements?statementId="+id, nil)
			req2.Header.Add("X-Experience-API-Version", "1.0.2")

			m.ServeHTTP(resp2, req2)

			if got, expected := resp2.Code, http.StatusOK; got != expected {
				t.Fatalf("Expected %v response code from get single statement; got %d", expected, got)
			}
		}
	}
}
Example #4
0
func main() {
	// start pprof (system profiler) server
	//go func() {
	//	logger.Info(http.ListenAndServe("localhost:6000", nil))
	//}()

	// start routing
	session, err := mgo.Dial(miscs.GlobalConfig.MongoDB.URL)
	defer session.Close()
	if err != nil {
		logger.Err(err)
		os.Exit(1)
	}
	model.InitDB(session.DB(miscs.GlobalConfig.MongoDB.DBName))

	c := controller.New(session)

	router := martini.Classic()
	router.Get("/", func() string { return "Welcome to xRS API Server." })

	// CROS support
	router.Options("**", func(params martini.Params, w http.ResponseWriter) {
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Header().Set("Access-Control-Allow-Origin", "*")
		w.Header().Set("Access-Control-Allow-Headers", "X-Experience-API-Version")
	})
	router.Get("/:user/:app/about", func(params martini.Params, w http.ResponseWriter) (int, string) {
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Header().Set("Access-Control-Allow-Origin", "*")
		w.Header().Set("Access-Control-Allow-Headers", "X-Experience-API-Version")

		return http.StatusOK, `{"version": ["1.0.0", "1.0.1", "1.0.2"]}`
	})
	router.Put("/:user/:app/statements", c.StoreStatement)
	router.Post("/:user/:app/statements", c.StoreMultStatement)
	router.Head("/:user/:app/statements", c.FindStatementHead)
	router.Get("/:user/:app/statements", acceptlang.Languages(), c.FindStatement)

	router.Run()
}