Exemplo n.º 1
0
func TestApplicationGuide(t *testing.T) {
	var a applicationGuide.ApplicationGuide
	var err error
	Convey("Testing ApplicationGuide", t, func() {
		//test create
		form := url.Values{"url": {"test"}, "fileType": {"pdf"}, "website_id": {"1"}}
		v := form.Encode()
		body := strings.NewReader(v)
		testThatHttp.Request("post", "/applicationGuide", "", "", CreateApplicationGuide, body, "application/x-www-form-urlencoded")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &a)
		So(err, ShouldBeNil)
		So(a, ShouldHaveSameTypeAs, applicationGuide.ApplicationGuide{})

		//test create using json
		var jsonAppGuide applicationGuide.ApplicationGuide
		jsonAppGuide.Url = "www.www.com"
		jsonAppGuide.FileType = "pdf"
		jsonAppGuide.Website.ID = 1
		bodyBytes, _ := json.Marshal(jsonAppGuide)
		bodyJson := bytes.NewReader(bodyBytes)
		testThatHttp.Request("post", "/applicationGuide", "", "", CreateApplicationGuide, bodyJson, "application/json")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &jsonAppGuide)
		So(err, ShouldBeNil)
		So(jsonAppGuide, ShouldHaveSameTypeAs, applicationGuide.ApplicationGuide{})

		//test get
		testThatHttp.Request("get", "/applicationGuide/", ":id", strconv.Itoa(a.ID), GetApplicationGuide, nil, "")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &a)
		So(err, ShouldBeNil)
		So(a, ShouldHaveSameTypeAs, applicationGuide.ApplicationGuide{})
		So(a.Url, ShouldEqual, "test")

		//test get by website
		testThatHttp.Request("get", "/applicationGuide/website/", ":id", strconv.Itoa(a.Website.ID), GetApplicationGuidesByWebsite, nil, "")
		var as []applicationGuide.ApplicationGuide
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &as)
		So(err, ShouldBeNil)
		So(len(as), ShouldBeGreaterThanOrEqualTo, 0)
		So(a.Url, ShouldEqual, "test")

		//test delete
		testThatHttp.Request("delete", "/applicationGuide/", ":id", strconv.Itoa(a.ID), DeleteApplicationGuide, nil, "")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &a)
		So(err, ShouldBeNil)

		//cleanup
		err = jsonAppGuide.Delete()

	})
}
Exemplo n.º 2
0
func CreateApplicationGuide(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	contType := req.Header.Get("Content-Type")

	var ag applicationGuide.ApplicationGuide
	var err error

	// if contType == "application/json" {
	if strings.Contains(contType, "application/json") {
		//json
		requestBody, err := ioutil.ReadAll(req.Body)
		if err != nil {
			apierror.GenerateError("Error reading request body", err, rw, req)
		}

		err = json.Unmarshal(requestBody, &ag)
		if err != nil {
			apierror.GenerateError("Error decoding request body", err, rw, req)
		}
	} else {
		//else, form
		ag.Url = req.FormValue("url")
		web := req.FormValue("website_id")
		ag.FileType = req.FormValue("file_type")
		cat := req.FormValue("category_id")

		if err != nil {
			apierror.GenerateError("Error parsing form", err, rw, req)
		}
		if web != "" {
			ag.Website.ID, err = strconv.Atoi(web)
		}
		if cat != "" {
			ag.Category.CategoryID, err = strconv.Atoi(cat)
		}
		if err != nil {
			apierror.GenerateError("Error parsing category ID or website ID", err, rw, req)
		}
	}
	err = ag.Create(dtx)
	if err != nil {
		apierror.GenerateError("Error creating Application Guide", err, rw, req)
	}

	//Return JSON
	return encoding.Must(enc.Encode(ag))
}