Ejemplo n.º 1
0
func TestContextReadJSON(t *testing.T) {
	iris.ResetDefault()
	iris.Post("/json", func(ctx *iris.Context) {
		obj := testBinderData{}
		err := ctx.ReadJSON(&obj)
		if err != nil {
			t.Fatalf("Error when parsing the JSON body: %s", err.Error())
		}
		ctx.JSON(iris.StatusOK, obj)
	})

	iris.Post("/json_pointer", func(ctx *iris.Context) {
		obj := &testBinderData{}
		err := ctx.ReadJSON(obj)
		if err != nil {
			t.Fatalf("Error when parsing the JSON body: %s", err.Error())
		}
		ctx.JSON(iris.StatusOK, obj)
	})

	e := httptest.New(iris.Default, t)
	passed := map[string]interface{}{"Username": "******", "Mail": "*****@*****.**", "mydata": []string{"mydata1", "mydata2"}}
	expectedObject := testBinderData{Username: "******", Mail: "*****@*****.**", Data: []string{"mydata1", "mydata2"}}

	e.POST("/json").WithJSON(passed).Expect().Status(iris.StatusOK).JSON().Object().Equal(expectedObject)
	e.POST("/json_pointer").WithJSON(passed).Expect().Status(iris.StatusOK).JSON().Object().Equal(expectedObject)
}
Ejemplo n.º 2
0
func TestContextReadXML(t *testing.T) {
	iris.ResetDefault()

	iris.Post("/xml", func(ctx *iris.Context) {
		obj := testBinderXMLData{}
		err := ctx.ReadXML(&obj)
		if err != nil {
			t.Fatalf("Error when parsing the XML body: %s", err.Error())
		}
		ctx.XML(iris.StatusOK, obj)
	})

	e := httptest.New(iris.Default, t)
	expectedObj := testBinderXMLData{
		XMLName:    xml.Name{Local: "info", Space: "info"},
		FirstAttr:  "this is the first attr",
		SecondAttr: "this is the second attr",
		Name:       "Iris web framework",
		Birth:      "13 March 2016",
		Stars:      4064,
	}
	// so far no WithXML or .XML like WithJSON and .JSON on httpexpect I added a feature request as post issue and we're waiting
	expectedBody := `<` + expectedObj.XMLName.Local + ` first="` + expectedObj.FirstAttr + `" second="` + expectedObj.SecondAttr + `"><name>` + expectedObj.Name + `</name><birth>` + expectedObj.Birth + `</birth><stars>` + strconv.Itoa(expectedObj.Stars) + `</stars></info>`
	e.POST("/xml").WithText(expectedBody).Expect().Status(iris.StatusOK).Body().Equal(expectedBody)
}
Ejemplo n.º 3
0
func main() {
	fmt.Println("Hello World!")
	iris.Get("/hi_json", func(c *iris.Context) {
		c.JSON(200, iris.Map{
			"Name": "Iris",
			"Age":  2,
			"名称":   "产品名称",
		})
	})
	/iris.Post()
	//iris.ListenTLS(":8080", "server.crt", "server.key")
}
Ejemplo n.º 4
0
func TestContextFormValueString(t *testing.T) {
	iris.ResetDefault()
	var k, v string
	k = "postkey"
	v = "postvalue"
	iris.Post("/", func(ctx *iris.Context) {
		ctx.Write(k + "=" + ctx.FormValueString(k))
	})
	e := httptest.New(iris.Default, t)

	e.POST("/").WithFormField(k, v).Expect().Status(iris.StatusOK).Body().Equal(k + "=" + v)
}
Ejemplo n.º 5
0
func TestContextReadJSONWithDecoder(t *testing.T) {
	iris.ResetDefault()
	iris.Post("/json_should_error", func(ctx *iris.Context) {
		obj := testJSONBinderDataWithDecoder{shouldError: true}
		err := ctx.ReadJSON(&obj)
		if err == nil {
			t.Fatalf("Should prompted for error 'Should error' but not error returned from the custom decoder!")
		}
		ctx.Write(err.Error())
		ctx.SetStatusCode(iris.StatusOK)
	})

	iris.Post("/json", func(ctx *iris.Context) {
		obj := testJSONBinderDataWithDecoder{}
		err := ctx.ReadJSON(&obj)
		if err != nil {
			t.Fatalf("Error when parsing the JSON body: %s", err.Error())
		}
		ctx.JSON(iris.StatusOK, obj)
	})

	iris.Post("/json_pointer", func(ctx *iris.Context) {
		obj := &testJSONBinderDataWithDecoder{}
		err := ctx.ReadJSON(obj)
		if err != nil {
			t.Fatalf("Error when parsing the JSON body: %s", err.Error())
		}
		ctx.JSON(iris.StatusOK, obj)
	})

	e := httptest.New(iris.Default, t)
	passed := map[string]interface{}{"Username": "******", "Mail": "*****@*****.**", "mydata": []string{"mydata1", "mydata2"}}
	expectedObject := testJSONBinderDataWithDecoder{Username: "******", Mail: "*****@*****.**", Data: []string{"mydata1", "mydata2"}}

	e.POST("/json_should_error").WithJSON(passed).Expect().Status(iris.StatusOK).Body().Equal("Should error")
	e.POST("/json").WithJSON(passed).Expect().Status(iris.StatusOK).JSON().Object().Equal(expectedObject)
	e.POST("/json_pointer").WithJSON(passed).Expect().Status(iris.StatusOK).JSON().Object().Equal(expectedObject)
} // no need for xml, it's exact the same.
Ejemplo n.º 6
0
func main() {
	flag.Parse()

	iris.Use(logger.New(iris.Logger()))
	iris.Use(cors.DefaultCors())

	iris.Get("/", controller.Index)
	iris.Get("/course/:courseID/:lessonID", controller.GetCourse)

	iris.Post("/submit/:classID/:courseID/:lessonID/:qn", controller.SubmitCode)

	fmt.Printf("listen on %s\n", *port)
	iris.Listen(*port)
}
Ejemplo n.º 7
0
func main() {
	dbinfo := fmt.Sprintf("port=32768 user=%s password=%s dbname=%s sslmode=disable", "postgres", "pass123", "postgres")
	db, err := sql.Open("postgres", dbinfo)
	if err != nil {
		fmt.Printf("err: %+v ", err)
	}
	defer db.Close()

	iris.Get("/people", GetAll(db))
	iris.Get("/people/:id", Get(db))
	iris.Post("/people/:id", Post(db))
	iris.Delete("/people/:id", Delete(db))

	iris.Listen(":8000")
}
Ejemplo n.º 8
0
func main() {
	mongoSession, pastas, err := database.NewPastasConnection()
	if err != nil {
		log.Fatal(err)
	}

	iris.UseTemplate(html.New(html.Config{
		Layout: "layout.html",
	})).Directory("./templates", ".html")

	iris.Static("/public", "./static", 1)

	iris.Get("/", func(ctx *iris.Context) {
		ctx.Render("home.html", Page{"Tonnarello", Pasta{"null", "null", "null"}}, iris.RenderOptions{"gzip": true})
	})

	iris.Post("/insert", func(ctx *iris.Context) {
		pasta := Pasta{}
		err := ctx.ReadForm(&pasta)
		if err != nil {
			fmt.Printf("ERR")
		}

		pasta.Id = bson.NewObjectId()

		err = pastas.Insert(pasta)
		if err != nil {
			log.Fatal(err)
		}

		ctx.Redirect("pasta/"+pasta.Id.Hex(), http.StatusSeeOther)
	})

	iris.Get("/pasta/:id", func(ctx *iris.Context) {
		objId := bson.ObjectIdHex(ctx.Param("id"))
		pasta := &Pasta{}

		pastas.FindId(objId).One(pasta)
		ctx.Render("pasta.html", Page{"Tonnarello", pasta}, iris.RenderOptions{"gzip": true})
	})

	iris.Listen(":4000")

	defer mongoSession.Close()
}
Ejemplo n.º 9
0
func TestContextSessions(t *testing.T) {
	t.Parallel()
	values := map[string]interface{}{
		"Name":   "iris",
		"Months": "4",
		"Secret": "dsads£2132215£%%Ssdsa",
	}

	iris.ResetDefault()
	iris.Default.Config.Sessions.Cookie = "mycustomsessionid"

	writeValues := func(ctx *iris.Context) {
		sessValues := ctx.Session().GetAll()
		ctx.JSON(iris.StatusOK, sessValues)
	}

	if testEnableSubdomain {
		iris.Party(testSubdomain+".").Get("/get", func(ctx *iris.Context) {
			writeValues(ctx)
		})
	}

	iris.Post("set", func(ctx *iris.Context) {
		vals := make(map[string]interface{}, 0)
		if err := ctx.ReadJSON(&vals); err != nil {
			t.Fatalf("Cannot readjson. Trace %s", err.Error())
		}
		for k, v := range vals {
			ctx.Session().Set(k, v)
		}
	})

	iris.Get("/get", func(ctx *iris.Context) {
		writeValues(ctx)
	})

	iris.Get("/clear", func(ctx *iris.Context) {
		ctx.Session().Clear()
		writeValues(ctx)
	})

	iris.Get("/destroy", func(ctx *iris.Context) {
		ctx.SessionDestroy()
		writeValues(ctx)
		// the cookie and all values should be empty
	})

	// request cookie should be empty
	iris.Get("/after_destroy", func(ctx *iris.Context) {
	})
	iris.Default.Config.VHost = "mydomain.com"
	e := httptest.New(iris.Default, t)

	e.POST("/set").WithJSON(values).Expect().Status(iris.StatusOK).Cookies().NotEmpty()
	e.GET("/get").Expect().Status(iris.StatusOK).JSON().Object().Equal(values)
	if testEnableSubdomain {
		es := subdomainTester(e)
		es.Request("GET", "/get").Expect().Status(iris.StatusOK).JSON().Object().Equal(values)
	}

	// test destroy which also clears first
	d := e.GET("/destroy").Expect().Status(iris.StatusOK)
	d.JSON().Null()
	// 	This removed: d.Cookies().Empty(). Reason:
	// httpexpect counts the cookies setted or deleted at the response time, but cookie is not removed, to be really removed needs to SetExpire(now-1second) so,
	// test if the cookies removed on the next request, like the browser's behavior.
	e.GET("/after_destroy").Expect().Status(iris.StatusOK).Cookies().Empty()
	// set and clear again
	e.POST("/set").WithJSON(values).Expect().Status(iris.StatusOK).Cookies().NotEmpty()
	e.GET("/clear").Expect().Status(iris.StatusOK).JSON().Object().Empty()
}