Best Syzkaller code snippet using main.serveHTTP
main_test.go
Source:main_test.go
1package main2import (3 //"fmt"4 "strings"5 //"reflect"6 "bytes"7 "io/ioutil"8 "net/http"9 "net/http/httptest"10 "testing"11 "github.com/stretchr/testify/assert"12)13//æ£è¦ã®sessionæ
å ±ãæ ¼ç´ãã14var mainCookie string = " "15//ãã°ã¤ã³ãã¨ã«sessionæ
å ±ãå
¥ãæ¿ãããã¨ããã¹ã16var oldCookie string = " "17//å¥ã¢ã«ã¦ã³ãã®ãã¹ãç¨18var subCookie string = " "19//æå¹æéåãã®cookieãæ±ãç¨20var tempCookie string = " "21//ããã¼ã®sessionæ
å ±22//jsonStr := `{"UserId":"dummy_user","Password":"dumdum00"}`23var dummyCookie string = `mysession=MTU5ODg0Mjg4NXxEdi1CQkFFQ180SUFBUkFCRUFBQU1fLUNBQUVHYzNSeWFXNW5EQXNBQ1ZObGMzTnBiMjVKUkFaemRISnBibWNNRWdBUU5UTkJTRmRwT1VoMllubFJlblF3ZEE9PXzqHFPM7diSqe2r0Kg2EzePlFc1iOf9Y2hBfzalMTSebA==; Path=/; Expires=Wed, 30 Sep 2020 03:01:25 GMT; Max-Age=2592000`24//ãµã¼ãã¼ã®ã«ã¼ãã£ã³ã°25var router = setupRouter()26//ãã¹ãã®ããã®meetingã¤ã³ã¹ã¿ã³ã¹27var GetSelectMeetingPageRoute string = "/"28var GetMeetingRoute string = "/meetings"29var PostMeetingRoute string = "/meetings"30var GetMinutesPageRoute string = "/meetings/1"31var DummyMinutesPageRoute string = "/meetings/1234"32var EntranceRoute string = "/entrance"33var GetUserInfo string = "/user"34var GetMessageRoute string = GetMinutesPageRoute + "/message"35var DummyGetMessgaeRoute string = DummyMinutesPageRoute + "/message"36var PostMessageRoute string = GetMinutesPageRoute + "/add_message"37var DummyPostMessgaeRoute string = DummyMinutesPageRoute + "/add_message"38var UpdateMessageRoute string = "/update_message"39var DeleteMessageRoute string = "/delete_message"40var LoginRoute string = "/login"41var RegisterRoute string = "/register"42var LogoutRoute string = "/logout"43var GetImportantWordsRoute string = GetMinutesPageRoute + "/important_words"44var GetImportantSentencesRoute string = GetMinutesPageRoute + "/important_sentences"45//ã¨ã³ãã©ã³ã¹ãã¼ã¸ã¯ã»ãã·ã§ã³æ
å ±ããªãã¦ãåå¾ã§ãã46func Test_entrancePage(t *testing.T) {47 //testRequestã®çµæãä¿åãããã¤48 resp := httptest.NewRecorder()49 //ãã¹ãã®ããã®http request50 req, _ := http.NewRequest("GET", EntranceRoute, nil)51 //requestããµã¼ãã¼ã«æµãã¦çµæãrespã«è¨é²52 router.ServeHTTP(resp, req)53 //bodyãåãåºã54 body, _ := ioutil.ReadAll(resp.Body)55 //ã¹ãã¼ã¿ã¹ã³ã¼ãã¯200ã®ã¯ã56 assert.Equal(t, 200, resp.Code)57 //titleã¯Entrance58 assert.Contains(t, string(body), "<title>Entrance</title>")59}60//loginãã¼ã¸ã¯ã»ãã·ã§ã³æ
å ±ããªãã¦ãåå¾ã§ãã61func Test_loginPage(t *testing.T) {62 //testRequestã®çµæãä¿åãããã¤63 resp := httptest.NewRecorder()64 //ãã¹ãã®ããã®http request65 req, _ := http.NewRequest("GET", LoginRoute, nil)66 //requestããµã¼ãã¼ã«æµãã¦çµæãrespã«è¨é²67 router.ServeHTTP(resp, req)68 //bodyãåãåºã69 body, _ := ioutil.ReadAll(resp.Body)70 //ã¹ãã¼ã¿ã¹ã³ã¼ãã¯200ã®ã¯ã71 assert.Equal(t, 200, resp.Code)72 assert.Contains(t, string(body), "<title>Login and Register</title>")73}74//ã¨ã³ãã©ã³ã¹ãã¼ã¸ã¯ã»ãã·ã§ã³æ
å ±ããªãã¦ãåå¾ã§ãã75func Test_registerPage(t *testing.T) {76 //testRequestã®çµæãä¿åãããã¤77 resp := httptest.NewRecorder()78 //ãã¹ãã®ããã®http request79 req, _ := http.NewRequest("GET", RegisterRoute, nil)80 //requestããµã¼ãã¼ã«æµãã¦çµæãrespã«è¨é²81 router.ServeHTTP(resp, req)82 //bodyãåãåºã83 body, _ := ioutil.ReadAll(resp.Body)84 //ã¹ãã¼ã¿ã¹ã³ã¼ãã¯200ã®ã¯ã85 assert.Equal(t, 200, resp.Code)86 assert.Contains(t, string(body), "<title>Login and Register</title>")87}88//idã¨passwordãããããï¼æå以ä¸ã®è±æ°åã ã¨ç»é²ã§ãã89func Test_canRegister_id_and_password_more8_and_alphanumeric(t *testing.T) {90 resp := httptest.NewRecorder()91 //éä¿¡ããjson92 jsonStr := `{"UserId":"test1234","Password":"qwer7890"}`93 req, _ := http.NewRequest(94 "POST",95 RegisterRoute,96 bytes.NewBuffer([]byte(jsonStr)),97 )98 // Content-Type è¨å®99 req.Header.Set("Content-Type", "application/json")100 router.ServeHTTP(resp, req)101 body, _ := ioutil.ReadAll(resp.Body)102 assert.Equal(t, 200, resp.Code)103 //é åºæ³¨æãassert.Contains 第äºå¼æ°ã«ç¬¬ä¸å¼æ°ã®è¦ç´ ãå«ã¾ãã¦ããã104 assert.Contains(t, string(body), "success")105}106//userIdã®éè¤ä¸å¯107func Test_cntRegister_same_id_and_password(t *testing.T) {108 resp := httptest.NewRecorder()109 //éä¿¡ããjson110 jsonStr := `{"UserId":"test1234","Password":"qwer7890"}`111 req, _ := http.NewRequest(112 "POST",113 RegisterRoute,114 bytes.NewBuffer([]byte(jsonStr)),115 )116 // Content-Type è¨å®117 req.Header.Set("Content-Type", "application/json")118 router.ServeHTTP(resp, req)119 body, _ := ioutil.ReadAll(resp.Body)120 assert.Equal(t, 400, resp.Code)121 //é åºæ³¨æãassert.Contains 第äºå¼æ°ã«ç¬¬ä¸å¼æ°ã®è¦ç´ ãå«ã¾ãã¦ããã122 assert.Contains(t, string(body), `error":"already use this id`)123}124//ãã¹ã¯ã¼ããåãå ´åã¯è¨±ã125func Test_canRegister_same_password(t *testing.T) {126 resp := httptest.NewRecorder()127 //éä¿¡ããjson128 jsonStr := `{"UserId":"test5678","Password":"qwer7890"}`129 req, _ := http.NewRequest(130 "POST",131 RegisterRoute,132 bytes.NewBuffer([]byte(jsonStr)),133 )134 // Content-Type è¨å®135 req.Header.Set("Content-Type", "application/json")136 router.ServeHTTP(resp, req)137 body, _ := ioutil.ReadAll(resp.Body)138 assert.Equal(t, 200, resp.Code)139 //é åºæ³¨æãassert.Contains 第äºå¼æ°ã«ç¬¬ä¸å¼æ°ã®è¦ç´ ãå«ã¾ãã¦ããã140 assert.Contains(t, string(body), "success")141}142//ææ¡143//ï¼æå以ä¸ã®userIdã¯è¨±ããªã144/*145func Test_cntRegister_id_less8(t *testing.T){146 resp := httptest.NewRecorder()147 //éä¿¡ããjson148 jsonStr := `{"UserId":"test567","Password":"qwer7890"}`149 req, _ := http.NewRequest(150 "POST",151 RegisterRoute,152 bytes.NewBuffer([]byte(jsonStr)),153 )154 // Content-Type è¨å®155 req.Header.Set("Content-Type", "application/json")156 router.ServeHTTP(resp, req)157 body, _ := ioutil.ReadAll(resp.Body)158 assert.Equal(t, 400, resp.Code)159 //é åºæ³¨æãassert.Contains 第äºå¼æ°ã«ç¬¬ä¸å¼æ°ã®è¦ç´ ãå«ã¾ãã¦ããã160 assert.Contains(t, string(body), `error":"ããæãã®ã¨ã©ã¼æ`)161}162*/163//ææ¡164//ï¼æå以ä¸ã®passwordã¯è¨±ããªã165/*166func Test_cntRegister_password_less8(t *testing.T){167 resp := httptest.NewRecorder()168 //éä¿¡ããjson169 jsonStr := `{"UserId":"test5678","Password":"qwer789"}`170 req, _ := http.NewRequest(171 "POST",172 RegisterRoute,173 bytes.NewBuffer([]byte(jsonStr)),174 )175 // Content-Type è¨å®176 req.Header.Set("Content-Type", "application/json")177 router.ServeHTTP(resp, req)178 body, _ := ioutil.ReadAll(resp.Body)179 assert.Equal(t, 400, resp.Code)180 //é åºæ³¨æãassert.Contains 第äºå¼æ°ã«ç¬¬ä¸å¼æ°ã®è¦ç´ ãå«ã¾ãã¦ããã181 assert.Contains(t, string(body), `error":"ããæãã®ã¨ã©ã¼æ`)182}183*/184//ææ¡185//è±åã®ã¿ã®passwordã¯è¨±ããªã186/*187func Test_cntRegister_password_only_alphabet(t *testing.T){188 resp := httptest.NewRecorder()189 //éä¿¡ããjson190 jsonStr := `{"UserId":"test5678","Password":"qwertest"}`191 req, _ := http.NewRequest(192 "POST",193 RegisterRoute,194 bytes.NewBuffer([]byte(jsonStr)),195 )196 // Content-Type è¨å®197 req.Header.Set("Content-Type", "application/json")198 router.ServeHTTP(resp, req)199 body, _ := ioutil.ReadAll(resp.Body)200 assert.Equal(t, 400, resp.Code)201 //é åºæ³¨æãassert.Contains 第äºå¼æ°ã«ç¬¬ä¸å¼æ°ã®è¦ç´ ãå«ã¾ãã¦ããã202 assert.Contains(t, string(body), `error":"ããæãã®ã¨ã©ã¼æ`)203}204*/205//ææ¡206//æ°åã®ã¿ã®passwordã¯è¨±ããªã207/*208func Test_cntRegister_password_only_num(t *testing.T){209 resp := httptest.NewRecorder()210 //éä¿¡ããjson211 jsonStr := `{"UserId":"test5678","Password":"11111111"}`212 req, _ := http.NewRequest(213 "POST",214 RegisterRoute,215 bytes.NewBuffer([]byte(jsonStr)),216 )217 // Content-Type è¨å®218 req.Header.Set("Content-Type", "application/json")219 router.ServeHTTP(resp, req)220 body, _ := ioutil.ReadAll(resp.Body)221 assert.Equal(t, 400, resp.Code)222 //é åºæ³¨æãassert.Contains 第äºå¼æ°ã«ç¬¬ä¸å¼æ°ã®è¦ç´ ãå«ã¾ãã¦ããã223 assert.Contains(t, string(body), `error":"ããæãã®ã¨ã©ã¼æ`)224}225*/226//ç»é²æ¸ã¿ã®ã¦ã¼ã¶ã¼ã¯ãã°ã¤ã³å¯è½227func Test_canLogin_registered_user(t *testing.T) {228 resp := httptest.NewRecorder()229 jsonStr := `{"UserId":"test1234","Password":"qwer7890"}`230 req, _ := http.NewRequest(231 "POST",232 LoginRoute,233 bytes.NewBuffer([]byte(jsonStr)),234 )235 // Content-Type è¨å®236 req.Header.Set("Content-Type", "application/json")237 router.ServeHTTP(resp, req)238 //fmt.Println(resp.Header().Get("Set-Cookie"))239 body, _ := ioutil.ReadAll(resp.Body)240 assert.Equal(t, 200, resp.Code)241 assert.Contains(t, string(body), "success")242 mainCookie = resp.Header().Get("Set-Cookie")243 oldCookie = resp.Header().Get("Set-Cookie")244}245//æªç»é²ã®ã¦ã¼ã¶ã¼ã§ã¯ãã°ã¤ã³ã§ããªã246func Test_cntLogin_not_registered_user(t *testing.T) {247 resp := httptest.NewRecorder()248 jsonStr := `{"UserId":"te344567","Password":"3wer3333"}`249 req, _ := http.NewRequest(250 "POST",251 LoginRoute,252 bytes.NewBuffer([]byte(jsonStr)),253 )254 // Content-Type è¨å®255 req.Header.Set("Content-Type", "application/json")256 router.ServeHTTP(resp, req)257 body, _ := ioutil.ReadAll(resp.Body)258 //fmt.Println(resp.Header().Get("Set-Cookie"))259 assert.Equal(t, 400, resp.Code)260 assert.Contains(t, string(body), `error":"user not exist`)261}262//ãã°ã¤ã³ããã«è°äºé²ä¸è¦§ãã¼ã¸ã«ã¯ãããªã263//ãªãã¤ã¬ã¯ã264func Test_redirect_meetingPage_not_logined(t *testing.T) {265 resp := httptest.NewRecorder()266 req, _ := http.NewRequest("GET", GetSelectMeetingPageRoute, nil)267 router.ServeHTTP(resp, req)268 assert.Equal(t, 303, resp.Code)269}270//ç»é²ããã¦ããªãã¦ã¼ã¶ã¼æ
å ±ãæã£ãsessionã§ã¯è°äºé²ä¸è¦§ãã¼ã¸ã«ã¢ã¯ã»ã¹ã§ããªã271func Test_cntAccess_meetingPage_dummySession(t *testing.T) {272 resp := httptest.NewRecorder()273 req, _ := http.NewRequest("GET", GetSelectMeetingPageRoute, nil)274 req.Header.Set("Cookie", dummyCookie)275 router.ServeHTTP(resp, req)276 //body, _ := ioutil.ReadAll(resp.Body)277 assert.Equal(t, 303, resp.Code)278 //é åºæ³¨æãassert.Contains 第äºå¼æ°ã«ç¬¬ä¸å¼æ°ã®è¦ç´ ãå«ã¾ãã¦ããã279 //assert.Contains(t, string(body), "Invalid session ID")280}281//ãã°ã¤ã³ãããªãè°äºé²ä¸è¦§ãã¼ã¸ã«è¡ãã282func Test_canAccess_meetingPage_logined(t *testing.T) {283 resp := httptest.NewRecorder()284 req, _ := http.NewRequest("GET", GetSelectMeetingPageRoute, nil)285 req.Header.Set("Cookie", mainCookie)286 router.ServeHTTP(resp, req)287 body, _ := ioutil.ReadAll(resp.Body)288 assert.Equal(t, 200, resp.Code)289 //é åºæ³¨æãassert.Contains 第äºå¼æ°ã«ç¬¬ä¸å¼æ°ã®è¦ç´ ãå«ã¾ãã¦ããã290 assert.Contains(t, string(body), "<title>Meetings</title>")291}292//ç»é²ãã¦ããªãã¦ã¼ã¶ã¼ã¯è°äºé²ä¸è¦§ãåå¾ä¸å¯293func Test_cntGetMeeting_not_logined_user(t *testing.T) {294 resp := httptest.NewRecorder()295 req, _ := http.NewRequest("GET", GetMeetingRoute, nil)296 router.ServeHTTP(resp, req)297 router.ServeHTTP(resp, req)298 body, _ := ioutil.ReadAll(resp.Body)299 assert.Equal(t, 400, resp.Code)300 assert.Contains(t, string(body), `error":"Bad Request`)301}302//ç»é²æ¸ã¿ã®ã¦ã¼ã¶ã¼ã¯è°äºé²ãä½æå¯è½303func Test_canAddMeeting_logined_user(t *testing.T) {304 resp := httptest.NewRecorder()305 jsonStr := `{"meeting":"è°äºé²ãã¹ã"}`306 req, _ := http.NewRequest(307 "POST",308 PostMeetingRoute,309 bytes.NewBuffer([]byte(jsonStr)),310 )311 req.Header.Set("Cookie", mainCookie)312 req.Header.Set("Content-Type", "application/json")313 router.ServeHTTP(resp, req)314 //fmt.Println(resp.Header().Get("Set-Cookie"))315 body, _ := ioutil.ReadAll(resp.Body)316 assert.Equal(t, 200, resp.Code)317 assert.Contains(t, string(body), "success")318 req, _ = http.NewRequest("GET", GetMeetingRoute, nil)319 req.Header.Set("Cookie", mainCookie)320 router.ServeHTTP(resp, req)321 body, _ = ioutil.ReadAll(resp.Body)322 assert.Equal(t, 200, resp.Code)323 assert.Contains(t, string(body), `[{"id":1,"name":"è°äºé²ãã¹ã"}]`)324}325//ååã®è°äºé²ã¯ä½æä¸å¯326func Test_cntAddMeeting_sameName(t *testing.T) {327 resp := httptest.NewRecorder()328 jsonStr := `{"meeting":"è°äºé²ãã¹ã"}`329 req, _ := http.NewRequest(330 "POST",331 PostMeetingRoute,332 bytes.NewBuffer([]byte(jsonStr)),333 )334 req.Header.Set("Cookie", mainCookie)335 req.Header.Set("Content-Type", "application/json")336 router.ServeHTTP(resp, req)337 //fmt.Println(resp.Header().Get("Set-Cookie"))338 body, _ := ioutil.ReadAll(resp.Body)339 assert.Equal(t, 400, resp.Code)340 assert.Contains(t, string(body), `error":"already use this name`)341}342//ç»é²ãã¦ããªãã¦ã¼ã¶ã¼ã¯è°äºé²ãä½æä¸å¯343func Test_cntAddMeeting_not_logined_user(t *testing.T) {344 resp := httptest.NewRecorder()345 jsonStr := `{"meeting":"è°äºé²ããã¼"}`346 req, _ := http.NewRequest(347 "POST",348 PostMeetingRoute,349 bytes.NewBuffer([]byte(jsonStr)),350 )351 req.Header.Set("Content-Type", "application/json")352 router.ServeHTTP(resp, req)353 //fmt.Println(resp.Header().Get("Set-Cookie"))354 body, _ := ioutil.ReadAll(resp.Body)355 assert.Equal(t, 400, resp.Code)356 assert.Contains(t, string(body), `error":"Bad Request`)357}358//ãã°ã¤ã³ããã«è°äºé²ãã¼ã¸ã«ã¯ãããªã359//ãªãã¤ã¬ã¯ã360func Test_redirect_minutesPage_not_logined(t *testing.T) {361 resp := httptest.NewRecorder()362 req, _ := http.NewRequest("GET", GetMinutesPageRoute, nil)363 router.ServeHTTP(resp, req)364 body, _ := ioutil.ReadAll(resp.Body)365 assert.Equal(t, 400, resp.Code)366 assert.Contains(t, string(body), `error":"Bad Request`)367}368//ç»é²ããã¦ããªãã¦ã¼ã¶ã¼æ
å ±ãæã£ãsessionã§ã¯ã¢ã¯ã»ã¹ã§ããªã369func Test_cntAccess_minutesPage_dummySession(t *testing.T) {370 resp := httptest.NewRecorder()371 req, _ := http.NewRequest("GET", GetMinutesPageRoute, nil)372 req.Header.Set("Cookie", dummyCookie)373 router.ServeHTTP(resp, req)374 body, _ := ioutil.ReadAll(resp.Body)375 assert.Equal(t, 400, resp.Code)376 //é åºæ³¨æãassert.Contains 第äºå¼æ°ã«ç¬¬ä¸å¼æ°ã®è¦ç´ ãå«ã¾ãã¦ããã377 assert.Contains(t, string(body), "Invalid session ID")378}379//ãã°ã¤ã³ãããªãè°äºé²ãã¼ã¸ã«è¡ãã380func Test_canAccess_minutesPage_logined(t *testing.T) {381 resp := httptest.NewRecorder()382 req, _ := http.NewRequest("GET", GetMinutesPageRoute, nil)383 req.Header.Set("Cookie", mainCookie)384 router.ServeHTTP(resp, req)385 body, _ := ioutil.ReadAll(resp.Body)386 assert.Equal(t, 200, resp.Code)387 //é åºæ³¨æãassert.Contains 第äºå¼æ°ã«ç¬¬ä¸å¼æ°ã®è¦ç´ ãå«ã¾ãã¦ããã388 assert.Contains(t, string(body), "<title>è°äºé²ãã¹ã</title>")389}390//åå¨ããªãè°äºé²ãã¼ã¸ã«ã¯ãããªã391func Test_cntAccess_minutesPage_notExist(t *testing.T) {392 resp := httptest.NewRecorder()393 req, _ := http.NewRequest("GET", DummyMinutesPageRoute, nil)394 req.Header.Set("Cookie", mainCookie)395 router.ServeHTTP(resp, req)396 body, _ := ioutil.ReadAll(resp.Body)397 assert.Equal(t, 404, resp.Code)398 assert.Contains(t, string(body), `error":"Not Found`)399}400//ãã°ã¢ã¦ãå¾ã«è°äºé²ãã¼ã¸ã«ãããªã401func Test_logout(t *testing.T) {402 resp := httptest.NewRecorder()403 req, _ := http.NewRequest("GET", LogoutRoute, nil)404 req.Header.Set("Cookie", mainCookie)405 router.ServeHTTP(resp, req)406 assert.Equal(t, 303, resp.Code)407 //é åºæ³¨æãassert.Contains 第äºå¼æ°ã«ç¬¬ä¸å¼æ°ã®è¦ç´ ãå«ã¾ãã¦ããã408 mainCookie = resp.Header().Get("Set-Cookie")409 //ã¡ããã© mysession = valueã®é¨åãåãåºãã410 assert.NotEqual(t, strings.Split(mainCookie, " ")[0], strings.Split(oldCookie, " ")[0])411 req, _ = http.NewRequest("GET", GetMinutesPageRoute, nil)412 req.Header.Set("Cookie", mainCookie)413 router.ServeHTTP(resp, req)414 assert.Equal(t, 303, resp.Code)415}416//ç»é²ãã¦ããªãã¦ã¼ã¶ã¼ã¯ã¡ãã»ã¼ã¸ãåå¾ä¸å¯417func Test_cntGetMessge_not_logined_user(t *testing.T) {418 resp := httptest.NewRecorder()419 req, _ := http.NewRequest("GET", GetMessageRoute, nil)420 router.ServeHTTP(resp, req)421 body, _ := ioutil.ReadAll(resp.Body)422 assert.Equal(t, 400, resp.Code)423 assert.Contains(t, string(body), `error":"Bad Request`)424}425//å度ãã°ã¤ã³ãå¿
è¦426//ç»é²æ¸ã¿ã®ã¦ã¼ã¶ã¼ã¯ãã°ã¤ã³å¯è½427//sessionæ
å ±ã¯æ¯åå¤ãã428func Test_canLogin_registered_user_useDifferentSessionInfo(t *testing.T) {429 resp := httptest.NewRecorder()430 jsonStr := `{"UserId":"test1234","Password":"qwer7890"}`431 req, _ := http.NewRequest(432 "POST",433 LoginRoute,434 bytes.NewBuffer([]byte(jsonStr)),435 )436 // Content-Type è¨å®437 req.Header.Set("Content-Type", "application/json")438 router.ServeHTTP(resp, req)439 //fmt.Println(resp.Header().Get("Set-Cookie"))440 body, _ := ioutil.ReadAll(resp.Body)441 assert.Equal(t, 200, resp.Code)442 assert.Contains(t, string(body), "success")443 //åãã¢ã«ã¦ã³ãã§ãæ¯åsessionæ
å ±ãå¤ãããã¨ã®ãã¹ã444 mainCookie = resp.Header().Get("Set-Cookie")445 //ã¡ããã© mysession = valueã®é¨åãåãåºãã446 assert.NotEqual(t, strings.Split(mainCookie, " ")[0], strings.Split(oldCookie, " ")[0])447}448//åå¨ããªãè°äºé²ã®ã¡ãã»ã¼ã¸ã¯åå¾ã§ããªã449func Test_cntGetMessage_notExistMinutes(t *testing.T) {450 resp := httptest.NewRecorder()451 req, _ := http.NewRequest("GET", DummyGetMessgaeRoute, nil)452 req.Header.Set("Cookie", mainCookie)453 router.ServeHTTP(resp, req)454 body, _ := ioutil.ReadAll(resp.Body)455 assert.Equal(t, 404, resp.Code)456 assert.Contains(t, string(body), `error":"Not Found`)457}458//åå¨ããªãè°äºé²ã¸ã¯ã¡ãã»ã¼ã¸ãéä¿¡ã§ããªã459func Test_cntPostMessage_notExistMinutes(t *testing.T) {460 resp := httptest.NewRecorder()461 jsonStr := `{"message":"ã«ã·ã¹ãªã¬ã³ã¸"}`462 req, _ := http.NewRequest(463 "POST",464 DummyPostMessgaeRoute,465 bytes.NewBuffer([]byte(jsonStr)),466 )467 req.Header.Set("Cookie", mainCookie)468 req.Header.Set("Content-Type", "application/json")469 router.ServeHTTP(resp, req)470 body, _ := ioutil.ReadAll(resp.Body)471 assert.Equal(t, 404, resp.Code)472 assert.Contains(t, string(body), `error":"Not Found`)473}474//ç»é²æ¸ã¿ã®ã¦ã¼ã¶ã¼ã¯ã¡ãã»ã¼ã¸ãéä¿¡å¯è½475func Test_canAddMessge_logined_user(t *testing.T) {476 resp := httptest.NewRecorder()477 jsonStr := `{"message":"ã«ã·ã¹ãªã¬ã³ã¸"}`478 req, _ := http.NewRequest(479 "POST",480 PostMessageRoute,481 bytes.NewBuffer([]byte(jsonStr)),482 )483 req.Header.Set("Cookie", mainCookie)484 req.Header.Set("Content-Type", "application/json")485 router.ServeHTTP(resp, req)486 //fmt.Println(resp.Header().Get("Set-Cookie"))487 body, _ := ioutil.ReadAll(resp.Body)488 assert.Equal(t, 200, resp.Code)489 assert.Contains(t, string(body), "success")490 req, _ = http.NewRequest("GET", GetMessageRoute, nil)491 req.Header.Set("Cookie", mainCookie)492 router.ServeHTTP(resp, req)493 body, _ = ioutil.ReadAll(resp.Body)494 assert.Equal(t, 200, resp.Code)495 assert.Contains(t, string(body), `[{"id":1,"addedBy":{"id":1,"name":"test1234"},"message":"ã«ã·ã¹ãªã¬ã³ã¸"}]`)496}497//ç»é²ãã¦ããªãã¦ã¼ã¶ã¼ã¯ã¡ãã»ã¼ã¸ãéä¿¡ä¸å¯498func Test_cntAddMessge_not_logined_user(t *testing.T) {499 resp := httptest.NewRecorder()500 jsonStr := `{"message":"ã¸ã³"}`501 req, _ := http.NewRequest(502 "POST",503 PostMessageRoute,504 bytes.NewBuffer([]byte(jsonStr)),505 )506 req.Header.Set("Content-Type", "application/json")507 router.ServeHTTP(resp, req)508 //fmt.Println(resp.Header().Get("Set-Cookie"))509 body, _ := ioutil.ReadAll(resp.Body)510 assert.Equal(t, 400, resp.Code)511 assert.Contains(t, string(body), `error":"Bad Request`)512}513//ãã¹ãã®ããã«ä»ã¦ã¼ã¶ã¼ã»ãã·ã§ã³ãåå¾514func Test_getSubCookie(t *testing.T) {515 resp := httptest.NewRecorder()516 //éä¿¡ããjson517 jsonStr := `{"UserId":"subA2222","Password":"qwegds890"}`518 req, _ := http.NewRequest(519 "POST",520 RegisterRoute,521 bytes.NewBuffer([]byte(jsonStr)),522 )523 // Content-Type è¨å®524 req.Header.Set("Content-Type", "application/json")525 router.ServeHTTP(resp, req)526 req, _ = http.NewRequest(527 "POST",528 LoginRoute,529 bytes.NewBuffer([]byte(jsonStr)),530 )531 // Content-Type è¨å®532 req.Header.Set("Content-Type", "application/json")533 router.ServeHTTP(resp, req)534 subCookie = resp.Header().Get("Set-Cookie")535}536//ç°ãªãã¦ã¼ã¶ã¼ã¯ã¡ãã»ã¼ã¸ãæ´æ°ä¸å¯537func Test_cntUpdateMessge_different_user(t *testing.T) {538 resp := httptest.NewRecorder()539 jsonStr := `{"id":"1","message":"ã¹ããã³ã°ã¼ã"}`540 req, _ := http.NewRequest(541 "POST",542 UpdateMessageRoute,543 bytes.NewBuffer([]byte(jsonStr)),544 )545 req.Header.Set("Cookie", subCookie)546 req.Header.Set("Content-Type", "application/json")547 router.ServeHTTP(resp, req)548 //fmt.Println(resp.Header().Get("Set-Cookie"))549 body, _ := ioutil.ReadAll(resp.Body)550 assert.Equal(t, 400, resp.Code)551 assert.Contains(t, string(body), `"error":"Malformed request due to privileges"`)552 resp = httptest.NewRecorder()553 req, _ = http.NewRequest("GET", GetMessageRoute, nil)554 req.Header.Set("Cookie", subCookie)555 router.ServeHTTP(resp, req)556 body, _ = ioutil.ReadAll(resp.Body)557 assert.Equal(t, 200, resp.Code)558 assert.Contains(t, string(body), `[{"id":1,"addedBy":{"id":1,"name":"test1234"},"message":"ã«ã·ã¹ãªã¬ã³ã¸"}]`)559 assert.NotContains(t, string(body), `[{"id":1,"addedBy":{"id":1,"name":"test1234"},"message":"ã¹ããã³ã°ã¼ã"}]`)560}561//ç»é²ãã¦ããªãã¦ã¼ã¶ã¼ã¯ã¡ãã»ã¼ã¸ãæ´æ°ä¸å¯562func Test_cntUpdateMessge_not_logined_user(t *testing.T) {563 resp := httptest.NewRecorder()564 jsonStr := `{"id":"1","message":"ã¹ããã³ã°ã¼ã"}`565 req, _ := http.NewRequest(566 "POST",567 UpdateMessageRoute,568 bytes.NewBuffer([]byte(jsonStr)),569 )570 req.Header.Set("Content-Type", "application/json")571 router.ServeHTTP(resp, req)572 body, _ := ioutil.ReadAll(resp.Body)573 assert.Equal(t, 400, resp.Code)574 assert.Contains(t, string(body), `"error":"Bad Request"`)575}576//åãã¦ã¼ã¶ã¼ã¯ã¡ãã»ã¼ã¸ãæ´æ°å¯è½577func Test_canUpdateMessge_same_user(t *testing.T) {578 resp := httptest.NewRecorder()579 jsonStr := `{"id":"1","message":"ã¹ããã³ã°ã¼ã"}`580 req, _ := http.NewRequest(581 "POST",582 UpdateMessageRoute,583 bytes.NewBuffer([]byte(jsonStr)),584 )585 req.Header.Set("Cookie", mainCookie)586 req.Header.Set("Content-Type", "application/json")587 router.ServeHTTP(resp, req)588 //fmt.Println(resp.Header().Get("Set-Cookie"))589 body, _ := ioutil.ReadAll(resp.Body)590 assert.Equal(t, 200, resp.Code)591 assert.Contains(t, string(body), "success")592 resp = httptest.NewRecorder()593 req, _ = http.NewRequest("GET", GetMessageRoute, nil)594 req.Header.Set("Cookie", mainCookie)595 router.ServeHTTP(resp, req)596 body, _ = ioutil.ReadAll(resp.Body)597 assert.Equal(t, 200, resp.Code)598 assert.Contains(t, string(body), `[{"id":1,"addedBy":{"id":1,"name":"test1234"},"message":"ã¹ããã³ã°ã¼ã"}]`)599 assert.NotContains(t, string(body), `[{"id":1,"addedBy":{"id":1,"name":"test1234"},"message":"ã«ã·ã¹ãªã¬ã³ã¸"}]`)600}601//ç°ãªãã¦ã¼ã¶ã¼ã¯ã¡ãã»ã¼ã¸ãåé¤ä¸å¯602func Test_cntDeleteMessge_different_user(t *testing.T) {603 resp := httptest.NewRecorder()604 jsonStr := `{"id":"1"}`605 req, _ := http.NewRequest(606 "POST",607 DeleteMessageRoute,608 bytes.NewBuffer([]byte(jsonStr)),609 )610 req.Header.Set("Cookie", subCookie)611 req.Header.Set("Content-Type", "application/json")612 router.ServeHTTP(resp, req)613 //fmt.Println(resp.Header().Get("Set-Cookie"))614 body, _ := ioutil.ReadAll(resp.Body)615 assert.Equal(t, 400, resp.Code)616 assert.Contains(t, string(body), `"error":"Malformed request due to privileges"`)617 resp = httptest.NewRecorder()618 req, _ = http.NewRequest("GET", GetMessageRoute, nil)619 req.Header.Set("Cookie", subCookie)620 router.ServeHTTP(resp, req)621 body, _ = ioutil.ReadAll(resp.Body)622 assert.Equal(t, 200, resp.Code)623 assert.Contains(t, string(body), `[{"id":1,"addedBy":{"id":1,"name":"test1234"},"message":"ã¹ããã³ã°ã¼ã"}]`)624}625//ç»é²ãã¦ããªãã¦ã¼ã¶ã¼ã¯ã¡ãã»ã¼ã¸ãåé¤ä¸å¯626func Test_cntDeleteMessge_not_logined_user(t *testing.T) {627 resp := httptest.NewRecorder()628 jsonStr := `{"id":"1"}`629 req, _ := http.NewRequest(630 "POST",631 DeleteMessageRoute,632 bytes.NewBuffer([]byte(jsonStr)),633 )634 req.Header.Set("Content-Type", "application/json")635 router.ServeHTTP(resp, req)636 //fmt.Println(resp.Header().Get("Set-Cookie"))637 body, _ := ioutil.ReadAll(resp.Body)638 assert.Equal(t, 400, resp.Code)639 assert.Contains(t, string(body), `"error":"Bad Request"`)640}641//åãã¦ã¼ã¶ã¼ã¯ã¡ãã»ã¼ã¸ãåé¤å¯è½642func Test_canDeleteMessge_same_user(t *testing.T) {643 resp := httptest.NewRecorder()644 jsonStr := `{"id":"1"}`645 req, _ := http.NewRequest(646 "POST",647 DeleteMessageRoute,648 bytes.NewBuffer([]byte(jsonStr)),649 )650 req.Header.Set("Cookie", mainCookie)651 req.Header.Set("Content-Type", "application/json")652 router.ServeHTTP(resp, req)653 //fmt.Println(resp.Header().Get("Set-Cookie"))654 body, _ := ioutil.ReadAll(resp.Body)655 assert.Equal(t, 200, resp.Code)656 assert.Contains(t, string(body), "success")657 resp = httptest.NewRecorder()658 req, _ = http.NewRequest("GET", GetMessageRoute, nil)659 req.Header.Set("Cookie", mainCookie)660 router.ServeHTTP(resp, req)661 body, _ = ioutil.ReadAll(resp.Body)662 assert.Equal(t, 200, resp.Code)663 assert.NotContains(t, string(body), `[{"id":1,"addedBy":{"id":1,"name":"test1234"},"message":"ã¹ããã³ã°ã¼ã"}]`)664}665//ãã°ã¤ã³ãã¦ããã¦ã¼ã¶ã¯éè¦åèªãåå¾ã§ãã666func Test_canGetImportantWords_logined_user(t *testing.T) {667 resp := httptest.NewRecorder()668 jsonStr := `{"message":"寿å¸ãé£ã¹ããã"}`669 req, _ := http.NewRequest(670 "POST",671 PostMessageRoute,672 bytes.NewBuffer([]byte(jsonStr)),673 )674 req.Header.Set("Cookie", mainCookie)675 req.Header.Set("Content-Type", "application/json")676 router.ServeHTTP(resp, req)677 //fmt.Println(resp.Header().Get("Set-Cookie"))678 body, _ := ioutil.ReadAll(resp.Body)679 assert.Equal(t, 200, resp.Code)680 assert.Contains(t, string(body), "success")681 req, _ = http.NewRequest("GET", GetImportantWordsRoute, nil)682 req.Header.Set("Cookie", mainCookie)683 router.ServeHTTP(resp, req)684 body, _ = ioutil.ReadAll(resp.Body)685 assert.Equal(t, 200, resp.Code)686 assert.Contains(t, string(body), `["ã","ã","ãã","寿å¸","é£ã¹"]`)687}688//ç»é²ãã¦ããªãã¦ã¼ã¶ã¼ã¯éè¦åèªã®åå¾ä¸å¯689func Test_cntGetImportantWords_not_logined_user(t *testing.T) {690 resp := httptest.NewRecorder()691 req, _ := http.NewRequest(692 "GET",693 GetImportantWordsRoute,694 nil,695 )696 req.Header.Set("Content-Type", "application/json")697 router.ServeHTTP(resp, req)698 body, _ := ioutil.ReadAll(resp.Body)699 assert.Equal(t, 400, resp.Code)700 assert.Contains(t, string(body), `"error":"Bad Request"`)701 //念ã®ãããå¾ã®ãã¹ãã®ããã«"寿å¸ãé£ã¹ããã"ã¯æ¶ãã¦ãã702 resp = httptest.NewRecorder()703 jsonStr := `{"id":"2"}`704 req, _ = http.NewRequest(705 "POST",706 DeleteMessageRoute,707 bytes.NewBuffer([]byte(jsonStr)),708 )709 req.Header.Set("Cookie", mainCookie)710 req.Header.Set("Content-Type", "application/json")711 router.ServeHTTP(resp, req)712 //fmt.Println(resp.Header().Get("Set-Cookie"))713 body, _ = ioutil.ReadAll(resp.Body)714 assert.Equal(t, 200, resp.Code)715 assert.Contains(t, string(body), "success")716 resp = httptest.NewRecorder()717 req, _ = http.NewRequest("GET", GetMessageRoute, nil)718 req.Header.Set("Cookie", mainCookie)719 router.ServeHTTP(resp, req)720 body, _ = ioutil.ReadAll(resp.Body)721 assert.Equal(t, 200, resp.Code)722 assert.NotContains(t, string(body), `[{"id":2,"addedBy":{"id":1,"name":"test1234"},"message":"寿å¸ãé£ã¹ããã"}]`)723}724//ãã°ã¤ã³ãã¦ãã人ã¯éè¦ãªæãåå¾ã§ãã725func Test_canGetImportantSentences_logined_user(t *testing.T) {726 resp := httptest.NewRecorder()727 jsonStr := `{"message":"æ¯æãæ¹æ³å¤æ´"}`728 req, _ := http.NewRequest(729 "POST",730 PostMessageRoute,731 bytes.NewBuffer([]byte(jsonStr)),732 )733 req.Header.Set("Cookie", mainCookie)734 req.Header.Set("Content-Type", "application/json")735 router.ServeHTTP(resp, req)736 body, _ := ioutil.ReadAll(resp.Body)737 assert.Equal(t, 200, resp.Code)738 assert.Contains(t, string(body), "success")739 jsonStr = `{"message":"å¥ã®æ¯æãæ¹æ³ãå¸æ"}`740 req, _ = http.NewRequest(741 "POST",742 PostMessageRoute,743 bytes.NewBuffer([]byte(jsonStr)),744 )745 req.Header.Set("Cookie", mainCookie)746 req.Header.Set("Content-Type", "application/json")747 router.ServeHTTP(resp, req)748 body, _ = ioutil.ReadAll(resp.Body)749 assert.Equal(t, 200, resp.Code)750 assert.Contains(t, string(body), "success")751 jsonStr = `{"message":"æ¯æãæ¹æ³å¤æ´ãé¡ããããã¾ã"}`752 req, _ = http.NewRequest(753 "POST",754 PostMessageRoute,755 bytes.NewBuffer([]byte(jsonStr)),756 )757 req.Header.Set("Cookie", mainCookie)758 req.Header.Set("Content-Type", "application/json")759 router.ServeHTTP(resp, req)760 body, _ = ioutil.ReadAll(resp.Body)761 assert.Equal(t, 200, resp.Code)762 assert.Contains(t, string(body), "success")763 jsonStr = `{"message":"ããããã«ã¼ãæ¯æãã¯ã§ããªãã®ã§ãããã å¥ã®æ¯æãæ¹æ¹æ³ã¯ãªãã®ã§ããããï¼"}`764 req, _ = http.NewRequest(765 "POST",766 PostMessageRoute,767 bytes.NewBuffer([]byte(jsonStr)),768 )769 req.Header.Set("Cookie", mainCookie)770 req.Header.Set("Content-Type", "application/json")771 router.ServeHTTP(resp, req)772 body, _ = ioutil.ReadAll(resp.Body)773 assert.Equal(t, 200, resp.Code)774 assert.Contains(t, string(body), "success")775 jsonStr = `{"message":"ã¯ã¬ã¸ããã«ã¼ãã§ã®æ¯æãã§ãããããã£ãã·ã³ã°è©æ¬ºã«ãã£ã¦ã¯ã¬ã¸ããã«ã¼ããåæ¢ãã¾ãããæ¯æãæ¹æ³ãå¤æ´ãã¦ã³ã³ããæ¯æãã«ãããã®ã§æ¯æãæ¹æ³ããç¥ãããã ããã"}`776 req, _ = http.NewRequest(777 "POST",778 PostMessageRoute,779 bytes.NewBuffer([]byte(jsonStr)),780 )781 req.Header.Set("Cookie", mainCookie)782 req.Header.Set("Content-Type", "application/json")783 router.ServeHTTP(resp, req)784 body, _ = ioutil.ReadAll(resp.Body)785 assert.Equal(t, 200, resp.Code)786 assert.Contains(t, string(body), "success")787 jsonStr = `{"message":"ãä¸è©±ã«ãªã£ã¦ããã¾ãã次åã®[製åå]ã®ãæ¯æãæ¹æ³ãã¯ã¬ã¸ããã«ã¼ãã«ãããã®ã§ããã[ä¸æºå
容]ãã®å
ããããã¾ããããè¿äºãé¡ããã¾ãã[æ°å]"}`788 req, _ = http.NewRequest(789 "POST",790 PostMessageRoute,791 bytes.NewBuffer([]byte(jsonStr)),792 )793 req.Header.Set("Cookie", mainCookie)794 req.Header.Set("Content-Type", "application/json")795 router.ServeHTTP(resp, req)796 body, _ = ioutil.ReadAll(resp.Body)797 assert.Equal(t, 200, resp.Code)798 assert.Contains(t, string(body), "success")799 req, _ = http.NewRequest("GET", GetImportantSentencesRoute, nil)800 req.Header.Set("Cookie", mainCookie)801 router.ServeHTTP(resp, req)802 body, _ = ioutil.ReadAll(resp.Body)803 assert.Equal(t, 200, resp.Code)804 assert.Contains(t, string(body), `["å¥ã®æ¯æãæ¹æ³ãå¸æ","ã¯ã¬ã¸ããã«ã¼ãã§ã®æ¯æãã§ãããããã£ãã·ã³ã°è©æ¬ºã«ãã£ã¦ã¯ã¬ã¸ããã«ã¼ããåæ¢ãã¾ãããæ¯æãæ¹æ³ãå¤æ´ãã¦ã³ã³ããæ¯æãã«ãããã®ã§æ¯æãæ¹æ³ããç¥ãããã ããã","æ¯æãæ¹æ³å¤æ´ãé¡ããããã¾ã","ãä¸è©±ã«ãªã£ã¦ããã¾ãã次åã®[製åå]ã®ãæ¯æãæ¹æ³ãã¯ã¬ã¸ããã«ã¼ãã«ãããã®ã§ããã[ä¸æºå
容]ãã®å
ããããã¾ããããè¿äºãé¡ããã¾ãã[æ°å]","æ¯æãæ¹æ³å¤æ´"]`)805}806//ç»é²ãã¦ããªãã¦ã¼ã¶ã¼ã¯éè¦æã®åå¾ä¸å¯807func Test_cntGetImportantSentences_not_logined_user(t *testing.T) {808 resp := httptest.NewRecorder()809 req, _ := http.NewRequest(810 "GET",811 GetImportantSentencesRoute,812 nil,813 )814 req.Header.Set("Content-Type", "application/json")815 router.ServeHTTP(resp, req)816 body, _ := ioutil.ReadAll(resp.Body)817 assert.Equal(t, 400, resp.Code)818 assert.Contains(t, string(body), `"error":"Bad Request"`)819 //念ã®ãããå¾ã®ãã¹ãã®ããã«åã«å
¥åããæç« ã¯æ¶ãã¦ãã820 resp = httptest.NewRecorder()821 jsonStr := `{"id":"3"}`822 req, _ = http.NewRequest(823 "POST",824 DeleteMessageRoute,825 bytes.NewBuffer([]byte(jsonStr)),826 )827 req.Header.Set("Cookie", mainCookie)828 req.Header.Set("Content-Type", "application/json")829 router.ServeHTTP(resp, req)830 //fmt.Println(resp.Header().Get("Set-Cookie"))831 body, _ = ioutil.ReadAll(resp.Body)832 assert.Equal(t, 200, resp.Code)833 assert.Contains(t, string(body), "success")834 resp = httptest.NewRecorder()835 req, _ = http.NewRequest("GET", GetMessageRoute, nil)836 req.Header.Set("Cookie", mainCookie)837 router.ServeHTTP(resp, req)838 body, _ = ioutil.ReadAll(resp.Body)839 assert.Equal(t, 200, resp.Code)840 assert.NotContains(t, string(body), `[{"id":3,"addedBy":{"id":1,"name":"test1234"},"message":"æ¯æãæ¹æ³å¤æ´"}]`)841 jsonStr = `{"id":"4"}`842 req, _ = http.NewRequest(843 "POST",844 DeleteMessageRoute,845 bytes.NewBuffer([]byte(jsonStr)),846 )847 req.Header.Set("Cookie", mainCookie)848 req.Header.Set("Content-Type", "application/json")849 router.ServeHTTP(resp, req)850 //fmt.Println(resp.Header().Get("Set-Cookie"))851 body, _ = ioutil.ReadAll(resp.Body)852 assert.Equal(t, 200, resp.Code)853 assert.Contains(t, string(body), "success")854 resp = httptest.NewRecorder()855 req, _ = http.NewRequest("GET", GetMessageRoute, nil)856 req.Header.Set("Cookie", mainCookie)857 router.ServeHTTP(resp, req)858 body, _ = ioutil.ReadAll(resp.Body)859 assert.Equal(t, 200, resp.Code)860 assert.NotContains(t, string(body), `[{"id":4,"addedBy":{"id":1,"name":"test1234"},"message":"å¥ã®æ¯æãæ¹æ³ãå¸æ"}]`)861 jsonStr = `{"id":"5"}`862 req, _ = http.NewRequest(863 "POST",864 DeleteMessageRoute,865 bytes.NewBuffer([]byte(jsonStr)),866 )867 req.Header.Set("Cookie", mainCookie)868 req.Header.Set("Content-Type", "application/json")869 router.ServeHTTP(resp, req)870 //fmt.Println(resp.Header().Get("Set-Cookie"))871 body, _ = ioutil.ReadAll(resp.Body)872 assert.Equal(t, 200, resp.Code)873 assert.Contains(t, string(body), "success")874 resp = httptest.NewRecorder()875 req, _ = http.NewRequest("GET", GetMessageRoute, nil)876 req.Header.Set("Cookie", mainCookie)877 router.ServeHTTP(resp, req)878 body, _ = ioutil.ReadAll(resp.Body)879 assert.Equal(t, 200, resp.Code)880 assert.NotContains(t, string(body), `[{"id":5,"addedBy":{"id":1,"name":"test1234"},"message":"æ¯æãæ¹æ³å¤æ´ãé¡ããããã¾ã"}]`)881 jsonStr = `{"id":"6"}`882 req, _ = http.NewRequest(883 "POST",884 DeleteMessageRoute,885 bytes.NewBuffer([]byte(jsonStr)),886 )887 req.Header.Set("Cookie", mainCookie)888 req.Header.Set("Content-Type", "application/json")889 router.ServeHTTP(resp, req)890 //fmt.Println(resp.Header().Get("Set-Cookie"))891 body, _ = ioutil.ReadAll(resp.Body)892 assert.Equal(t, 200, resp.Code)893 assert.Contains(t, string(body), "success")894 resp = httptest.NewRecorder()895 req, _ = http.NewRequest("GET", GetMessageRoute, nil)896 req.Header.Set("Cookie", mainCookie)897 router.ServeHTTP(resp, req)898 body, _ = ioutil.ReadAll(resp.Body)899 assert.Equal(t, 200, resp.Code)900 assert.NotContains(t, string(body), `[{"id":6,"addedBy":{"id":1,"name":"test1234"},"message":"ããããã«ã¼ãæ¯æãã¯ã§ããªãã®ã§ãããã å¥ã®æ¯æãæ¹æ¹æ³ã¯ãªãã®ã§ããããï¼"}]`)901 jsonStr = `{"id":"7"}`902 req, _ = http.NewRequest(903 "POST",904 DeleteMessageRoute,905 bytes.NewBuffer([]byte(jsonStr)),906 )907 req.Header.Set("Cookie", mainCookie)908 req.Header.Set("Content-Type", "application/json")909 router.ServeHTTP(resp, req)910 //fmt.Println(resp.Header().Get("Set-Cookie"))911 body, _ = ioutil.ReadAll(resp.Body)912 assert.Equal(t, 200, resp.Code)913 assert.Contains(t, string(body), "success")914 resp = httptest.NewRecorder()915 req, _ = http.NewRequest("GET", GetMessageRoute, nil)916 req.Header.Set("Cookie", mainCookie)917 router.ServeHTTP(resp, req)918 body, _ = ioutil.ReadAll(resp.Body)919 assert.Equal(t, 200, resp.Code)920 assert.NotContains(t, string(body), `[{"id":7,"addedBy":{"id":1,"name":"test1234"},"message":"ã¯ã¬ã¸ããã«ã¼ãã§ã®æ¯æãã§ãããããã£ãã·ã³ã°è©æ¬ºã«ãã£ã¦ã¯ã¬ã¸ããã«ã¼ããåæ¢ãã¾ãããæ¯æãæ¹æ³ãå¤æ´ãã¦ã³ã³ããæ¯æãã«ãããã®ã§æ¯æãæ¹æ³ããç¥ãããã ããã"}]`)921 jsonStr = `{"id":"8"}`922 req, _ = http.NewRequest(923 "POST",924 DeleteMessageRoute,925 bytes.NewBuffer([]byte(jsonStr)),926 )927 req.Header.Set("Cookie", mainCookie)928 req.Header.Set("Content-Type", "application/json")929 router.ServeHTTP(resp, req)930 //fmt.Println(resp.Header().Get("Set-Cookie"))931 body, _ = ioutil.ReadAll(resp.Body)932 assert.Equal(t, 200, resp.Code)933 assert.Contains(t, string(body), "success")934 resp = httptest.NewRecorder()935 req, _ = http.NewRequest("GET", GetMessageRoute, nil)936 req.Header.Set("Cookie", mainCookie)937 router.ServeHTTP(resp, req)938 body, _ = ioutil.ReadAll(resp.Body)939 assert.Equal(t, 200, resp.Code)940 assert.NotContains(t, string(body), `[{"id":8,"addedBy":{"id":1,"name":"test1234"},"message":"ãä¸è©±ã«ãªã£ã¦ããã¾ãã次åã®[製åå]ã®ãæ¯æãæ¹æ³ãã¯ã¬ã¸ããã«ã¼ãã«ãããã®ã§ããã[ä¸æºå
容]ãã®å
ããããã¾ããããè¿äºãé¡ããã¾ãã[æ°å]"}]`)941}942//ãã°ã¤ã³ãã¦ããªãã¦ã¼ã¶ã¼ã¯ã¦ã¼ã¶ã¼æ
å ±ã¯å¸°ããªã943func Test_cntGetUserInfo_not_logined_user(t *testing.T) {944 resp := httptest.NewRecorder()945 req, _ := http.NewRequest("GET", GetUserInfo, nil)946 router.ServeHTTP(resp, req)947 //fmt.Println(resp.Header().Get("Set-Cookie"))948 body, _ := ioutil.ReadAll(resp.Body)949 assert.Equal(t, 400, resp.Code)950 assert.Contains(t, string(body), `"error":"Bad Request"`)951}952//ãã°ã¤ã³ãã¦ããã¦ã¼ã¶ã¼ã¯ã¦ã¼ã¶ã¼æ
å ±ã帰ã953//ã»ãã¥ãªãã£çã«å¤§ä¸å¤«ãï¼954func Test_canGetUserInfo_logined_user(t *testing.T) {955 resp := httptest.NewRecorder()956 req, _ := http.NewRequest("GET", GetUserInfo, nil)957 req.Header.Set("Cookie", mainCookie)958 router.ServeHTTP(resp, req)959 //fmt.Println(resp.Header().Get("Set-Cookie"))960 body, _ := ioutil.ReadAll(resp.Body)961 assert.Equal(t, 200, resp.Code)962 assert.Contains(t, string(body), `{"id":1,"name":"test1234"}`)963}964//æå¹æéåãã®sessionIDã®å©ç¨ããã¹ãããã¦ã¼ã¶ãä½æ965func Test_Login_registered_user_tempUse00(t *testing.T) {966 resp := httptest.NewRecorder()967 //éä¿¡ããjson968 jsonStr := `{"UserId":"temp00","Password":"temptemp00"}`969 req, _ := http.NewRequest(970 "POST",971 RegisterRoute,972 bytes.NewBuffer([]byte(jsonStr)),973 )974 // Content-Type è¨å®975 req.Header.Set("Content-Type", "application/json")976 router.ServeHTTP(resp, req)977 req, _ = http.NewRequest(978 "POST",979 LoginRoute,980 bytes.NewBuffer([]byte(jsonStr)),981 )982 // Content-Type è¨å®983 req.Header.Set("Content-Type", "application/json")984 router.ServeHTTP(resp, req)985 tempCookie = resp.Header().Get("Set-Cookie")986}987//æéåãã®ã»ãã·ã§ã³ã¯é©å®ãã¼ã¿ãã¼ã¹ããç ´æ£ããã¦ãã988func Test_session_database_update(t *testing.T) {989 //DBã®sessionæ
å ±ãæéåãã«ãªãããã«ç¡çããè¨å®990 sessionTimeSet10daysLater("temp00")991 resp := httptest.NewRecorder()992 req, _ := http.NewRequest("GET", GetMinutesPageRoute, nil)993 req.Header.Set("Cookie", tempCookie)994 router.ServeHTTP(resp, req)995 body, _ := ioutil.ReadAll(resp.Body)996 assert.Equal(t, 400, resp.Code)997 //é åºæ³¨æãassert.Contains 第äºå¼æ°ã«ç¬¬ä¸å¼æ°ã®è¦ç´ ãå«ã¾ãã¦ããã998 assert.Contains(t, string(body), "Invalid session ID")999}1000//æå¹æéåãã®sessionIDã®å©ç¨ããã¹ãããã¦ã¼ã¶ãä½æ1001func Test_Login_registered_user_tempUse01(t *testing.T) {1002 resp := httptest.NewRecorder()1003 //éä¿¡ããjson1004 jsonStr := `{"UserId":"temp00","Password":"temptemp00"}`1005 req, _ := http.NewRequest(1006 "POST",1007 RegisterRoute,1008 bytes.NewBuffer([]byte(jsonStr)),1009 )1010 // Content-Type è¨å®1011 req.Header.Set("Content-Type", "application/json")1012 router.ServeHTTP(resp, req)1013 req, _ = http.NewRequest(1014 "POST",1015 LoginRoute,1016 bytes.NewBuffer([]byte(jsonStr)),1017 )1018 // Content-Type è¨å®1019 req.Header.Set("Content-Type", "application/json")1020 router.ServeHTTP(resp, req)1021 tempCookie = resp.Header().Get("Set-Cookie")1022}1023//æéåãã®ã»ãã·ã§ã³ã¯ã»ãã·ã§ã³ã¿ã¤ã ã¢ã¦ãã«ãªã1024func Test_session_timeout(t *testing.T) {1025 //DBã®sessionæ
å ±ãæéåãã«ãªãããã«ç¡çããè¨å®1026 sessionTimeSetNow("temp00")1027 resp := httptest.NewRecorder()1028 req, _ := http.NewRequest("GET", GetMinutesPageRoute, nil)1029 req.Header.Set("Cookie", tempCookie)1030 router.ServeHTTP(resp, req)1031 body, _ := ioutil.ReadAll(resp.Body)1032 assert.Equal(t, 400, resp.Code)1033 //é åºæ³¨æãassert.Contains 第äºå¼æ°ã«ç¬¬ä¸å¼æ°ã®è¦ç´ ãå«ã¾ãã¦ããã1034 assert.Contains(t, string(body), "Session time out")1035}1036//middlewareå
å
¨ã¦ã®å¦çã¯ç»é²ãã¦ããªãã¦ã¼ã¶ã¯åãä»ããªã**å¿
ãlogedInå
ã®ãªã¯ã¨ã¹ã§ãã¹ããããã¨**1037//ç»é²ãã¦ããªãã¦ã¼ã¶ã¼ã¯ã¡ãã»ã¼ã¸ãéä¿¡ä¸å¯1038func Test_middeleware_logedIn_not_logined_user(t *testing.T) {1039 resp := httptest.NewRecorder()1040 jsonStr := `{"message":"ã¸ã³"}`1041 req, _ := http.NewRequest(1042 "POST",1043 PostMessageRoute,1044 bytes.NewBuffer([]byte(jsonStr)),1045 )1046 req.Header.Set("Content-Type", "application/json")1047 router.ServeHTTP(resp, req)1048 body, _ := ioutil.ReadAll(resp.Body)1049 assert.Equal(t, 400, resp.Code)1050 assert.Contains(t, string(body), `error":"Bad Request`)1051}1052//middlewareå
å
¨ã¦ã®å¦çã¯ä¸å½ãªã»ãã·ã§ã³IDã¯åãä»ããªã**å¿
ãlogedInå
ã®ãªã¯ã¨ã¹ã§ãã¹ããããã¨**1053//ä¸å½ãªã»ãã·ã§ã³IDã®ã¦ã¼ã¶ã¼ã¯ã¡ãã»ã¼ã¸ãéä¿¡ä¸å¯1054func Test_middeleware_logedIn_invalid_user(t *testing.T) {1055 resp := httptest.NewRecorder()1056 jsonStr := `{"message":"ã¸ã³"}`1057 req, _ := http.NewRequest(1058 "POST",1059 PostMessageRoute,1060 bytes.NewBuffer([]byte(jsonStr)),1061 )1062 req.Header.Set("Cookie", dummyCookie)1063 req.Header.Set("Content-Type", "application/json")1064 router.ServeHTTP(resp, req)1065 body, _ := ioutil.ReadAll(resp.Body)1066 assert.Equal(t, 400, resp.Code)1067 assert.Contains(t, string(body), "Invalid session ID")1068}1069//æå¹æéåãã®sessionIDã®å©ç¨ããã¹ãããã¦ã¼ã¶ãä½æ1070func Test_Login_registered_user_tempUse02(t *testing.T) {1071 resp := httptest.NewRecorder()1072 //éä¿¡ããjson1073 jsonStr := `{"UserId":"temp00","Password":"temptemp00"}`1074 req, _ := http.NewRequest(1075 "POST",1076 RegisterRoute,1077 bytes.NewBuffer([]byte(jsonStr)),1078 )1079 // Content-Type è¨å®1080 req.Header.Set("Content-Type", "application/json")1081 router.ServeHTTP(resp, req)1082 req, _ = http.NewRequest(1083 "POST",1084 LoginRoute,1085 bytes.NewBuffer([]byte(jsonStr)),1086 )1087 // Content-Type è¨å®1088 req.Header.Set("Content-Type", "application/json")1089 router.ServeHTTP(resp, req)1090 tempCookie = resp.Header().Get("Set-Cookie")1091}1092//middlewareå
å
¨ã¦ã®å¦çã¯æå¹æéã®åããã»ãã·ã§ã³IDã¯åãä»ããªã**å¿
ãlogedInå
ã®ãªã¯ã¨ã¹ã§ãã¹ããããã¨**1093//æå¹æéåãã®ã¦ã¼ã¶ã¼ã¯ã¡ãã»ã¼ã¸ãéä¿¡ä¸å¯1094func Test_middeleware_logedIn_session_timeout(t *testing.T) {1095 sessionTimeSetNow("temp00")1096 resp := httptest.NewRecorder()1097 jsonStr := `{"message":"ã¸ã³"}`1098 req, _ := http.NewRequest(1099 "POST",1100 PostMessageRoute,1101 bytes.NewBuffer([]byte(jsonStr)),1102 )1103 req.Header.Set("Cookie", tempCookie)1104 req.Header.Set("Content-Type", "application/json")1105 router.ServeHTTP(resp, req)1106 body, _ := ioutil.ReadAll(resp.Body)1107 assert.Equal(t, 400, resp.Code)1108 assert.Contains(t, string(body), "Session time out")1109}...
server_test.go
Source:server_test.go
1package server2import (3 "bytes"4 "encoding/json"5 "net/http"6 "net/http/httptest"7 "testing"8 "github.com/stretchr/testify/suite"9 "gorm.io/gorm"10 "github.com/jeandeducla/api-plant/internal/models"11 "github.com/jeandeducla/api-plant/internal/plants"12)13type MainTestSuite struct {14 suite.Suite15 service *plants.Service16 server *Server17 db *gorm.DB18}19func TestServer(t *testing.T) {20 suite.Run(t, new(MainTestSuite))21}22func (t *MainTestSuite) SetupTest() {23 db, err := models.NewDB("postgres://postgres:postgres@postgresql/metron")24 t.Require().NoError(err)25 t.db = db26 service := plants.NewPlantsService(plants.NewPlantsDB(db))27 t.service = service28 server, err := NewServer(service)29 t.Require().NoError(err)30 t.server = server31}32func (t *MainTestSuite) TearDownTest() {33 t.db.Migrator().DropTable(&models.Asset{})34 t.db.Migrator().DropTable(&models.Plant{})35 t.db.Migrator().DropTable(&models.EnergyManager{})36}37func (t *MainTestSuite) TestGetAllEnergyManagers() {38 // No ems should not return an error and res should be an empty slice39 {40 w := httptest.NewRecorder()41 req, _ := http.NewRequest("GET", "/ems", nil)42 t.server.Router().ServeHTTP(w, req)43 t.Equal(200, w.Code)44 var res []models.EnergyManager45 t.Require().NoError(json.NewDecoder(w.Result().Body).Decode(&res))46 t.Equal(len(res), 0)47 }48 // There are some ems49 {50 body := []byte(`51 {52 "name": "coucou",53 "surname": "salut"54 }55 `)56 w := httptest.NewRecorder()57 req, _ := http.NewRequest("POST", "/ems", bytes.NewReader(body))58 t.server.Router().ServeHTTP(w, req)59 t.Equal(200, w.Code)60 req, _ = http.NewRequest("GET", "/ems", nil)61 t.server.Router().ServeHTTP(w, req)62 var res []models.EnergyManager63 t.Require().NoError(json.NewDecoder(w.Result().Body).Decode(&res))64 t.Equal(len(res), 1)65 t.Equal(res[0].Name, "coucou")66 }67}68func (t *MainTestSuite) TestPostEnergyManager() {69 // empty body70 w := httptest.NewRecorder()71 req, _ := http.NewRequest("POST", "/ems", nil)72 t.server.Router().ServeHTTP(w, req)73 t.Equal(400, w.Code)74 // body is missing parts75 body := []byte(`76 {77 "name": "coucou",78 }79 `)80 w = httptest.NewRecorder()81 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))82 t.server.Router().ServeHTTP(w, req)83 t.Equal(400, w.Code)84 // body is correct85 body = []byte(`86 {87 "name": "coucou",88 "surname": "salut"89 }90 `)91 w = httptest.NewRecorder()92 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))93 t.server.Router().ServeHTTP(w, req)94 t.Equal(200, w.Code)95 // body has more fields than expected96 body = []byte(`97 {98 "name": "Jacques",99 "surname": "salut",100 "useless": 1101 }102 `)103 w = httptest.NewRecorder()104 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))105 t.server.Router().ServeHTTP(w, req)106 t.Equal(200, w.Code)107 {108 w := httptest.NewRecorder()109 req, _ := http.NewRequest("GET", "/ems", nil)110 t.server.Router().ServeHTTP(w, req)111 t.Equal(200, w.Code)112 var res []models.EnergyManager113 t.Require().NoError(json.NewDecoder(w.Result().Body).Decode(&res))114 t.Equal(len(res), 2)115 }116 {117 w := httptest.NewRecorder()118 req, _ = http.NewRequest("GET", "/ems/2", nil)119 t.server.Router().ServeHTTP(w, req)120 t.Equal(200, w.Code)121 var res models.EnergyManager122 t.Require().NoError(json.NewDecoder(w.Result().Body).Decode(&res))123 t.Equal(res.Name, "Jacques")124 }125}126func (t *MainTestSuite) TestGetEnergyManager() {127 // id is not parsable to uint128 w := httptest.NewRecorder()129 req, _ := http.NewRequest("GET", "/ems/hjki", nil)130 t.server.Router().ServeHTTP(w, req)131 t.Equal(404, w.Code)132 // no id133 w = httptest.NewRecorder()134 req, _ = http.NewRequest("GET", "/ems/", nil)135 t.server.Router().ServeHTTP(w, req)136 t.Equal(301, w.Code)137 // id does not exist138 w = httptest.NewRecorder()139 req, _ = http.NewRequest("GET", "/ems/1234", nil)140 t.server.Router().ServeHTTP(w, req)141 t.Equal(404, w.Code)142 // id does exist143 body := []byte(`144 {145 "name": "coucou",146 "surname": "salut"147 }148 `)149 w = httptest.NewRecorder()150 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))151 t.server.Router().ServeHTTP(w, req)152 t.Equal(200, w.Code)153 w = httptest.NewRecorder()154 req, _ = http.NewRequest("GET", "/ems/1", nil)155 t.server.Router().ServeHTTP(w, req)156 t.Equal(200, w.Code)157 // bad http verb158 w = httptest.NewRecorder()159 req, _ = http.NewRequest("OPTIONS", "/ems/2", nil)160 t.server.Router().ServeHTTP(w, req)161 t.Equal(404, w.Code)162}163func (t *MainTestSuite) TestDeleteEnergyManager() {164 // id is not parsable to uint165 w := httptest.NewRecorder()166 req, _ := http.NewRequest("DELETE", "/ems/jemangedupoulet", nil)167 t.server.Router().ServeHTTP(w, req)168 t.Equal(404, w.Code)169 // id does not exist170 w = httptest.NewRecorder()171 req, _ = http.NewRequest("DELETE", "/ems/4", nil)172 t.server.Router().ServeHTTP(w, req)173 t.Equal(404, w.Code)174 // id does exist175 body := []byte(`176 {177 "name": "coucou",178 "surname": "salut"179 }180 `)181 w = httptest.NewRecorder()182 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))183 t.server.Router().ServeHTTP(w, req)184 t.Equal(200, w.Code)185 w = httptest.NewRecorder()186 req, _ = http.NewRequest("DELETE", "/ems/1", nil)187 t.server.Router().ServeHTTP(w, req)188 t.Equal(200, w.Code)189 w = httptest.NewRecorder()190 req, _ = http.NewRequest("GET", "/ems/1", nil)191 t.server.Router().ServeHTTP(w, req)192 t.Equal(404, w.Code)193}194func (t *MainTestSuite) TestPutEnergyManager() {195 // id is not parsable to uint196 w := httptest.NewRecorder()197 req, _ := http.NewRequest("PUT", "/ems/^jlliu", nil)198 t.server.Router().ServeHTTP(w, req)199 t.Equal(404, w.Code)200 // id does not exist and request body is valid201 body := []byte(`202 {203 "name": "coucou",204 "surname": "salut"205 }206 `)207 w = httptest.NewRecorder()208 req, _ = http.NewRequest("PUT", "/ems/4", bytes.NewReader(body))209 t.server.Router().ServeHTTP(w, req)210 t.Equal(404, w.Code)211 // request body is empty212 w = httptest.NewRecorder()213 req, _ = http.NewRequest("PUT", "/ems/1", nil)214 t.server.Router().ServeHTTP(w, req)215 t.Equal(400, w.Code)216 // request body is not valid217 body = []byte(`218 {219 "name": "coucou",220 "surname": "salut",221 }222 `)223 w = httptest.NewRecorder()224 req, _ = http.NewRequest("PUT", "/ems/1", bytes.NewReader(body))225 t.server.Router().ServeHTTP(w, req)226 t.Equal(400, w.Code)227 // request body has useless fields228 body = []byte(`229 {230 "name": "coucou",231 "surname": "salut"232 }233 `)234 w = httptest.NewRecorder()235 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))236 t.server.Router().ServeHTTP(w, req)237 t.Equal(200, w.Code)238 w = httptest.NewRecorder()239 body = []byte(`240 {241 "name": "Jacques",242 "surname": "salut",243 "roger": "moore"244 }245 `)246 w = httptest.NewRecorder()247 req, _ = http.NewRequest("PUT", "/ems/1", bytes.NewReader(body))248 t.server.Router().ServeHTTP(w, req)249 t.Equal(200, w.Code)250 w = httptest.NewRecorder()251 req, _ = http.NewRequest("GET", "/ems/1", bytes.NewReader(body))252 t.server.Router().ServeHTTP(w, req)253 t.Equal(200, w.Code)254 var res models.EnergyManager255 t.Require().NoError(json.NewDecoder(w.Result().Body).Decode(&res))256 t.Equal(res.Name, "Jacques")257}258func (t *MainTestSuite) TestGetEnergyManagerPlants() {259 // id is not parsable to uint260 w := httptest.NewRecorder()261 req, _ := http.NewRequest("PUT", "/ems/^jlliu/plants", nil)262 t.server.Router().ServeHTTP(w, req)263 t.Equal(404, w.Code)264 // id does not exist265 w = httptest.NewRecorder()266 req, _ = http.NewRequest("PUT", "/ems/1/plants", nil)267 t.server.Router().ServeHTTP(w, req)268 t.Equal(404, w.Code)269 // no plants should return empty slice of plants270 body := []byte(`271 {272 "name": "Gerard",273 "surname": "Depardieu"274 }275 `)276 w = httptest.NewRecorder()277 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))278 t.server.Router().ServeHTTP(w, req)279 t.Equal(200, w.Code)280 w = httptest.NewRecorder()281 req, _ = http.NewRequest("GET", "/ems/1/plants", nil)282 t.server.Router().ServeHTTP(w, req)283 t.Equal(200, w.Code)284 var res []models.Plant285 t.Require().NoError(json.NewDecoder(w.Result().Body).Decode(&res))286 t.Equal(len(res), 0)287 // Some plants 288 body = []byte(`289 {290 "name": "Gerard",291 "address": "187 rue triuy",292 "max_power": 189,293 "energy_manager_id": 1294 }295 `)296 w = httptest.NewRecorder()297 req, _ = http.NewRequest("POST", "/plants", bytes.NewReader(body))298 t.server.Router().ServeHTTP(w, req)299 t.Equal(200, w.Code)300 req, _ = http.NewRequest("GET", "/ems/1/plants", nil)301 t.server.Router().ServeHTTP(w, req)302 t.Equal(200, w.Code)303 {304 var res []models.Plant305 t.Require().NoError(json.NewDecoder(w.Result().Body).Decode(&res))306 t.Equal(len(res), 1)307 t.Equal(res[0].Name, "Gerard")308 }309}310func (t *MainTestSuite) TestGetPlants() {311 // no plants should return empty slice312 w := httptest.NewRecorder()313 req, _ := http.NewRequest("GET", "/plants", nil)314 t.server.Router().ServeHTTP(w, req)315 t.Equal(200, w.Code)316 var res []models.Plant317 t.Require().NoError(json.NewDecoder(w.Result().Body).Decode(&res))318 t.Equal(len(res), 0)319 // Some plant320 body := []byte(`321 {322 "name": "Gerard",323 "surname": "Depardieu"324 }325 `)326 w = httptest.NewRecorder()327 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))328 t.server.Router().ServeHTTP(w, req)329 t.Equal(200, w.Code)330 body = []byte(`331 {332 "name": "Gerard",333 "address": "187 rue triuy",334 "max_power": 189,335 "energy_manager_id": 1336 }337 `)338 w = httptest.NewRecorder()339 req, _ = http.NewRequest("POST", "/plants", bytes.NewReader(body))340 t.server.Router().ServeHTTP(w, req)341 t.Equal(200, w.Code)342 w = httptest.NewRecorder()343 req, _ = http.NewRequest("GET", "/plants", nil)344 t.server.Router().ServeHTTP(w, req)345 t.Equal(200, w.Code)346 {347 var res []models.Plant348 t.Require().NoError(json.NewDecoder(w.Result().Body).Decode(&res))349 t.Equal(len(res), 1)350 t.Equal(res[0].Name, "Gerard")351 }352}353func (t *MainTestSuite) TestPostPlants() {354 // empty body355 w := httptest.NewRecorder()356 req, _ := http.NewRequest("POST", "/plants", nil)357 t.server.Router().ServeHTTP(w, req)358 t.Equal(400, w.Code)359 // bad body360 body := []byte(`361 {362 "name": "Gerard",363 "surname": "Depardieu"364 }365 `)366 w = httptest.NewRecorder()367 req, _ = http.NewRequest("POST", "/plants", bytes.NewReader(body))368 t.server.Router().ServeHTTP(w, req)369 t.Equal(400, w.Code)370 // bad body371 body = []byte(`372 {373 "name": "Gerard",374 "Address": 1,375 "max)Power": 376 }377 `)378 w = httptest.NewRecorder()379 req, _ = http.NewRequest("POST", "/plants", bytes.NewReader(body))380 t.server.Router().ServeHTTP(w, req)381 t.Equal(400, w.Code)382 // can't create plant if em does not exist383 body = []byte(`384 {385 "name": "Gerard",386 "address": "187 rue triuy",387 "max_power": 189,388 "energy_manager_id": 1389 }390 `)391 w = httptest.NewRecorder()392 req, _ = http.NewRequest("POST", "/plants", bytes.NewReader(body))393 t.server.Router().ServeHTTP(w, req)394 t.Equal(400, w.Code)395 // works396 body = []byte(`397 {398 "name": "Gerard",399 "surname": "Bond"400 }401 `)402 w = httptest.NewRecorder()403 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))404 t.server.Router().ServeHTTP(w, req)405 t.Equal(200, w.Code)406 body = []byte(`407 {408 "name": "Plant",409 "address": "187 rue triuy",410 "max_power": 189,411 "energy_manager_id": 1412 }413 `)414 w = httptest.NewRecorder()415 req, _ = http.NewRequest("POST", "/plants", bytes.NewReader(body))416 t.server.Router().ServeHTTP(w, req)417 t.Equal(200, w.Code)418 w = httptest.NewRecorder()419 req, _ = http.NewRequest("GET", "/plants/1", bytes.NewReader(body))420 t.server.Router().ServeHTTP(w, req)421 t.Equal(200, w.Code)422 var res models.Plant423 t.Require().NoError(json.NewDecoder(w.Result().Body).Decode(&res))424 t.Equal(res.Name, "Plant")425}426func (t *MainTestSuite) TestDeletePlant() {427 // id is not parsable428 w := httptest.NewRecorder()429 req, _ := http.NewRequest("DELETE", "/plants/joel", nil)430 t.server.Router().ServeHTTP(w, req)431 t.Equal(404, w.Code)432 // id does not exist433 w = httptest.NewRecorder()434 req, _ = http.NewRequest("DELETE", "/plants/1", nil)435 t.server.Router().ServeHTTP(w, req)436 t.Equal(404, w.Code)437 // works438 body := []byte(`439 {440 "name": "Gerard",441 "surname": "Depardieu"442 }443 `)444 w = httptest.NewRecorder()445 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))446 t.server.Router().ServeHTTP(w, req)447 t.Equal(200, w.Code)448 body = []byte(`449 {450 "name": "Plant",451 "address": "187 rue triuy",452 "max_power": 189,453 "energy_manager_id": 1454 }455 `)456 w = httptest.NewRecorder()457 req, _ = http.NewRequest("POST", "/plants", bytes.NewReader(body))458 t.server.Router().ServeHTTP(w, req)459 t.Equal(200, w.Code)460 w = httptest.NewRecorder()461 req, _ = http.NewRequest("DELETE", "/plants/1", nil)462 t.server.Router().ServeHTTP(w, req)463 t.Equal(200, w.Code)464 w = httptest.NewRecorder()465 req, _ = http.NewRequest("GET", "/plants/1", nil)466 t.server.Router().ServeHTTP(w, req)467 t.Equal(404, w.Code)468 w = httptest.NewRecorder()469 req, _ = http.NewRequest("GET", "/ems/1/plants", nil)470 t.server.Router().ServeHTTP(w, req)471 t.Equal(200, w.Code)472 var res []models.Plant473 t.Require().NoError(json.NewDecoder(w.Result().Body).Decode(&res))474 t.Equal(len(res), 0)475}476func (t *MainTestSuite) TestPutPlant() {477 // id is not parsable478 w := httptest.NewRecorder()479 req, _ := http.NewRequest("PUT", "/plants/joel", nil)480 t.server.Router().ServeHTTP(w, req)481 t.Equal(404, w.Code)482 // id does not exist483 body := []byte(`484 {485 "name": "Plant",486 "address": "187 rue triuy",487 "max_power": 189,488 "energy_manager_id": 1489 }490 `)491 w = httptest.NewRecorder()492 req, _ = http.NewRequest("PUT", "/plants/1", bytes.NewReader(body))493 t.server.Router().ServeHTTP(w, req)494 t.Equal(404, w.Code)495 // invalid body496 body = []byte(`497 {498 "name": "Plant",499 "address": "187 rue triuy",500 "max_Power": 189501 "energy_manager_id": 1502 }503 `)504 w = httptest.NewRecorder()505 req, _ = http.NewRequest("PUT", "/plants/1", bytes.NewReader(body))506 t.server.Router().ServeHTTP(w, req)507 t.Equal(400, w.Code)508 // update to em that does not exist509 body = []byte(`510 {511 "name": "Eric",512 "surname": "judor"513 }514 `)515 w = httptest.NewRecorder()516 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))517 t.server.Router().ServeHTTP(w, req)518 t.Equal(200, w.Code)519 body = []byte(`520 {521 "name": "Plant",522 "address": "187 rue triuy",523 "max_power": 189,524 "energy_manager_id": 1525 }526 `)527 w = httptest.NewRecorder()528 req, _ = http.NewRequest("POST", "/plants", bytes.NewReader(body))529 t.server.Router().ServeHTTP(w, req)530 t.Equal(200, w.Code)531 body = []byte(`532 {533 "name": "Plant",534 "address": "187 rue triuy",535 "max_power": 189,536 "energy_manager_id": 1123537 }538 `)539 w = httptest.NewRecorder()540 req, _ = http.NewRequest("PUT", "/plants/1", bytes.NewReader(body))541 t.server.Router().ServeHTTP(w, req)542 t.Equal(400, w.Code)543}544func (t *MainTestSuite) TestGetPlantAssets() {545 // id is not parsable546 w := httptest.NewRecorder()547 req, _ := http.NewRequest("GET", "/plants/joel/assets", nil)548 t.server.Router().ServeHTTP(w, req)549 t.Equal(404, w.Code)550 // id does not exist551 w = httptest.NewRecorder()552 req, _ = http.NewRequest("GET", "/plants/1/assets", nil)553 t.server.Router().ServeHTTP(w, req)554 t.Equal(404, w.Code)555 // No assets = no errors and empty list556 body := []byte(`557 {558 "name": "Eric",559 "surname": "judor"560 }561 `)562 w = httptest.NewRecorder()563 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))564 t.server.Router().ServeHTTP(w, req)565 t.Equal(200, w.Code)566 body = []byte(`567 {568 "name": "Plant",569 "address": "187 rue triuy",570 "max_power": 189,571 "energy_manager_id": 1572 }573 `)574 w = httptest.NewRecorder()575 req, _ = http.NewRequest("POST", "/plants", bytes.NewReader(body))576 t.server.Router().ServeHTTP(w, req)577 t.Equal(200, w.Code)578 w = httptest.NewRecorder()579 req, _ = http.NewRequest("GET", "/plants/1/assets", nil)580 t.server.Router().ServeHTTP(w, req)581 t.Equal(200, w.Code)582 {583 var res []models.Asset584 t.Require().NoError(json.NewDecoder(w.Result().Body).Decode(&res))585 t.Equal(len(res), 0)586 }587 // Some assets588 body = []byte(`589 {590 "name": "asset",591 "max_power": 189,592 "type": "furnace",593 "plant_id": 1594 }595 `)596 w = httptest.NewRecorder()597 req, _ = http.NewRequest("POST", "/plants/1/assets", bytes.NewReader(body))598 t.server.Router().ServeHTTP(w, req)599 t.Equal(200, w.Code)600 w = httptest.NewRecorder()601 req, _ = http.NewRequest("GET", "/plants/1/assets", nil)602 t.server.Router().ServeHTTP(w, req)603 t.Equal(200, w.Code)604 {605 var res []models.Asset606 t.Require().NoError(json.NewDecoder(w.Result().Body).Decode(&res))607 t.Equal(len(res), 1)608 t.Equal(res[0].Name, "asset")609 }610}611func (t *MainTestSuite) TestPostPlantAssets() {612 // id is not parsable613 w := httptest.NewRecorder()614 req, _ := http.NewRequest("POST", "/plants/joel/assets", nil)615 t.server.Router().ServeHTTP(w, req)616 t.Equal(404, w.Code)617 // id does not exist618 body := []byte(`619 {620 "name": "asset",621 "max_power": 10,622 "type": "furnace"623 }624 `)625 w = httptest.NewRecorder()626 req, _ = http.NewRequest("POST", "/plants/1/assets", bytes.NewReader(body))627 t.server.Router().ServeHTTP(w, req)628 t.Equal(404, w.Code)629 // bad body630 body = []byte(`631 {632 "name": "Eric",633 "surname": "judor"634 }635 `)636 w = httptest.NewRecorder()637 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))638 t.server.Router().ServeHTTP(w, req)639 t.Equal(200, w.Code)640 body = []byte(`641 {642 "name": "Plant",643 "address": "187 rue triuy",644 "max_power": 189,645 "energy_manager_id": 1646 }647 `)648 w = httptest.NewRecorder()649 req, _ = http.NewRequest("POST", "/plants", bytes.NewReader(body))650 t.server.Router().ServeHTTP(w, req)651 t.Equal(200, w.Code)652 body = []byte(`653 {654 "name": "asset",655 "max_power": 10,656 "coucou": "furnace"657 }658 `)659 w = httptest.NewRecorder()660 req, _ = http.NewRequest("POST", "/plants/1/assets", bytes.NewReader(body))661 t.server.Router().ServeHTTP(w, req)662 t.Equal(400, w.Code)663 // wrong type664 body = []byte(`665 {666 "name": "asset",667 "max_power": 10,668 "type": "JACK CHIRAK"669 }670 `)671 w = httptest.NewRecorder()672 req, _ = http.NewRequest("POST", "/plants/1/assets", bytes.NewReader(body))673 t.server.Router().ServeHTTP(w, req)674 t.Equal(400, w.Code)675 // plant id does not exist676 body = []byte(`677 {678 "name": "asset",679 "max_power": 10,680 "type": "chiller"681 }682 `)683 w = httptest.NewRecorder()684 req, _ = http.NewRequest("POST", "/plants/1123/assets", bytes.NewReader(body))685 t.server.Router().ServeHTTP(w, req)686 t.Equal(404, w.Code)687 688 // power limit is broken689 body = []byte(`690 {691 "name": "asset",692 "max_power": 1000000,693 "type": "chiller"694 }695 `)696 w = httptest.NewRecorder()697 req, _ = http.NewRequest("POST", "/plants/1/assets", bytes.NewReader(body))698 t.server.Router().ServeHTTP(w, req)699 t.Equal(400, w.Code)700}701func (t *MainTestSuite) TestDeletePlantAsset() {702 // id is not parsable703 w := httptest.NewRecorder()704 req, _ := http.NewRequest("DELETE", "/plants/joel/assets", nil)705 t.server.Router().ServeHTTP(w, req)706 t.Equal(404, w.Code)707 // id does not exist708 w = httptest.NewRecorder()709 req, _ = http.NewRequest("DELETE", "/plants/1/assets/1", nil)710 t.server.Router().ServeHTTP(w, req)711 t.Equal(404, w.Code)712 // id does not exist 2713 body := []byte(`714 {715 "name": "Eric",716 "surname": "judor"717 }718 `)719 w = httptest.NewRecorder()720 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))721 t.server.Router().ServeHTTP(w, req)722 t.Equal(200, w.Code)723 body = []byte(`724 {725 "name": "Plant",726 "address": "187 rue triuy",727 "max_power": 189,728 "energy_manager_id": 1729 }730 `)731 w = httptest.NewRecorder()732 req, _ = http.NewRequest("POST", "/plants", bytes.NewReader(body))733 t.server.Router().ServeHTTP(w, req)734 t.Equal(200, w.Code)735 w = httptest.NewRecorder()736 req, _ = http.NewRequest("DELETE", "/plants/1/assets/1", nil)737 t.server.Router().ServeHTTP(w, req)738 t.Equal(404, w.Code)739 // id does exist740 body = []byte(`741 {742 "name": "asset",743 "max_power": 10,744 "type": "furnace"745 }746 `)747 w = httptest.NewRecorder()748 req, _ = http.NewRequest("POST", "/plants/1/assets", bytes.NewReader(body))749 t.server.Router().ServeHTTP(w, req)750 t.Equal(200, w.Code)751 w = httptest.NewRecorder()752 req, _ = http.NewRequest("DELETE", "/plants/1/assets/1", nil)753 t.server.Router().ServeHTTP(w, req)754 t.Equal(200, w.Code)755 w = httptest.NewRecorder()756 req, _ = http.NewRequest("GET", "/plants/1/assets/1", nil)757 t.server.Router().ServeHTTP(w, req)758 t.Equal(404, w.Code)759}760func (t *MainTestSuite) TestPutPlantAsset() {761 // id is not parsable762 w := httptest.NewRecorder()763 req, _ := http.NewRequest("PUT", "/plants/joel/assets", nil)764 t.server.Router().ServeHTTP(w, req)765 t.Equal(404, w.Code)766 // id does not exist767 body := []byte(`768 {769 "name": "asset",770 "max_power": 10,771 "type": "furnace"772 }773 `)774 w = httptest.NewRecorder()775 req, _ = http.NewRequest("PUT", "/plants/1/assets/1", bytes.NewReader(body))776 t.server.Router().ServeHTTP(w, req)777 t.Equal(404, w.Code)778 // id does not exist 2779 body = []byte(`780 {781 "name": "Eric",782 "surname": "judor"783 }784 `)785 w = httptest.NewRecorder()786 req, _ = http.NewRequest("POST", "/ems", bytes.NewReader(body))787 t.server.Router().ServeHTTP(w, req)788 t.Equal(200, w.Code)789 body = []byte(`790 {791 "name": "Plant",792 "address": "187 rue triuy",793 "max_power": 189,794 "energy_manager_id": 1795 }796 `)797 w = httptest.NewRecorder()798 req, _ = http.NewRequest("POST", "/plants", bytes.NewReader(body))799 t.server.Router().ServeHTTP(w, req)800 t.Equal(200, w.Code)801 body = []byte(`802 {803 "name": "asset",804 "max_power": 10,805 "type": "furnace"806 }807 `)808 w = httptest.NewRecorder()809 req, _ = http.NewRequest("PUT", "/plants/1/assets/1", bytes.NewReader(body))810 t.server.Router().ServeHTTP(w, req)811 t.Equal(404, w.Code)812 // bad body813 body = []byte(`814 {815 "name": "asset",816 "max_power": 10,817 "type": "furnace"818 }819 `)820 w = httptest.NewRecorder()821 req, _ = http.NewRequest("POST", "/plants/1/assets", bytes.NewReader(body))822 t.server.Router().ServeHTTP(w, req)823 t.Equal(200, w.Code)824 body = []byte(`825 {826 "name": "asset",827 "max_power": 10,828 "coucou": "furnace"829 }830 `)831 w = httptest.NewRecorder()832 req, _ = http.NewRequest("PUT", "/plants/1/assets/1", bytes.NewReader(body))833 t.server.Router().ServeHTTP(w, req)834 t.Equal(400, w.Code)835 // update to bad type836 body = []byte(`837 {838 "name": "asset",839 "max_power": 10,840 "type": "JACK CHIRAK"841 }842 `)843 w = httptest.NewRecorder()844 req, _ = http.NewRequest("PUT", "/plants/1/assets/1", bytes.NewReader(body))845 t.server.Router().ServeHTTP(w, req)846 t.Equal(400, w.Code)847 // update breaks power limit848 body = []byte(`849 {850 "name": "asset",851 "max_power": 100000,852 "type": "furnace"853 }854 `)855 w = httptest.NewRecorder()856 req, _ = http.NewRequest("PUT", "/plants/1/assets/1", bytes.NewReader(body))857 t.server.Router().ServeHTTP(w, req)858 t.Equal(400, w.Code)859}...
gss_test.go
Source:gss_test.go
1package main2import (3 "net/http"4 "net/http/httptest"5 "testing"6 "github.com/stretchr/testify/assert"7)8func TestGSS(t *testing.T) {9 metrics := registerMetrics()10 t.Run("uses the provided headers", func(t *testing.T) {11 t.Parallel()12 cfg := &config{13 Headers: map[string]string{14 "X-Test": "test",15 },16 }17 fileServer := newFileServer(cfg, metrics).init()18 w := httptest.NewRecorder()19 r := httptest.NewRequest(http.MethodGet, "/", nil)20 fileServer.Server.Handler.ServeHTTP(w, r)21 assert.Equal(t, "test", w.Header().Get("X-Test"))22 })23 t.Run("redirects index correctly", func(t *testing.T) {24 t.Parallel()25 cfg := &config{}26 fileServer := newFileServer(cfg, metrics).init()27 w := httptest.NewRecorder()28 r := httptest.NewRequest(http.MethodGet, "/index.html", nil)29 fileServer.Server.Handler.ServeHTTP(w, r)30 assert.Equal(t, http.StatusMovedPermanently, w.Code)31 })32 t.Run("serves HTML files succesfully", func(t *testing.T) {33 t.Parallel()34 cfg := &config{}35 fileServer := newFileServer(cfg, metrics).init()36 w := httptest.NewRecorder()37 r := httptest.NewRequest(http.MethodGet, "/", nil)38 fileServer.Server.Handler.ServeHTTP(w, r)39 assert.Equal(t, http.StatusOK, w.Code)40 assert.Equal(t, "/", r.RequestURI)41 assert.Contains(t, w.Header().Get("Content-Type"), "html")42 })43 t.Run("serves CSS files succesfully", func(t *testing.T) {44 t.Parallel()45 cfg := &config{}46 fileServer := newFileServer(cfg, metrics).init()47 w := httptest.NewRecorder()48 r := httptest.NewRequest(http.MethodGet, "/main.68aa49f7.css", nil)49 fileServer.Server.Handler.ServeHTTP(w, r)50 assert.Equal(t, http.StatusOK, w.Result().StatusCode)51 assert.Equal(t, "/main.68aa49f7.css", r.RequestURI)52 assert.Contains(t, w.Header().Get("Content-Type"), "css")53 })54 t.Run("serves JavaScript files succesfully", func(t *testing.T) {55 t.Parallel()56 cfg := &config{}57 fileServer := newFileServer(cfg, metrics).init()58 w := httptest.NewRecorder()59 r := httptest.NewRequest(http.MethodGet, "/main.8d3db4ef.js", nil)60 fileServer.Server.Handler.ServeHTTP(w, r)61 assert.Equal(t, http.StatusOK, w.Result().StatusCode)62 assert.Equal(t, "/main.8d3db4ef.js", r.RequestURI)63 assert.Contains(t, w.Header().Get("Content-Type"), "javascript")64 })65 t.Run("serves other files succesfully", func(t *testing.T) {66 t.Parallel()67 cfg := &config{}68 fileServer := newFileServer(cfg, metrics).init()69 w := httptest.NewRecorder()70 r := httptest.NewRequest(http.MethodGet, "/main.8d3db4ef.js.LICENSE.txt", nil)71 fileServer.Server.Handler.ServeHTTP(w, r)72 assert.Equal(t, http.StatusOK, w.Result().StatusCode)73 assert.Equal(t, "/main.8d3db4ef.js.LICENSE.txt", r.RequestURI)74 })75 t.Run("serves brotli files succesfully", func(t *testing.T) {76 t.Parallel()77 cfg := &config{}78 fileServer := newFileServer(cfg, metrics).init()79 w := httptest.NewRecorder()80 r := httptest.NewRequest(http.MethodGet, "/main.8d3db4ef.js", nil)81 r.Header.Add("Accept-Encoding", "br")82 fileServer.Server.Handler.ServeHTTP(w, r)83 assert.Equal(t, http.StatusOK, w.Code)84 assert.Equal(t, "br", w.Header().Get("Content-Encoding"))85 })86 t.Run("serves gzip files succesfully", func(t *testing.T) {87 t.Parallel()88 cfg := &config{}89 fileServer := newFileServer(cfg, metrics).init()90 w := httptest.NewRecorder()91 r := httptest.NewRequest(http.MethodGet, "/main.8d3db4ef.js", nil)92 r.Header.Add("Accept-Encoding", "gzip")93 fileServer.Server.Handler.ServeHTTP(w, r)94 assert.Equal(t, http.StatusOK, w.Code)95 assert.Equal(t, "gzip", w.Header().Get("Content-Encoding"))96 })97 t.Run("serves unexisting files without extension", func(t *testing.T) {98 t.Parallel()99 cfg := &config{}100 fileServer := newFileServer(cfg, metrics).init()101 w := httptest.NewRecorder()102 r := httptest.NewRequest(http.MethodGet, "/random-page", nil)103 fileServer.Server.Handler.ServeHTTP(w, r)104 assert.Equal(t, http.StatusOK, w.Code)105 assert.Equal(t, "/random-page", r.RequestURI)106 assert.Contains(t, w.Header().Get("Content-Type"), "html")107 })108 t.Run("doesn't serve unexisting files with extension", func(t *testing.T) {109 t.Parallel()110 cfg := &config{}111 fileServer := newFileServer(cfg, metrics).init()112 w := httptest.NewRecorder()113 r := httptest.NewRequest(http.MethodGet, "/favicon.ico", nil)114 fileServer.Server.Handler.ServeHTTP(w, r)115 assert.Equal(t, http.StatusNotFound, w.Code)116 })117 t.Run("serves a cached response for a fresh resource", func(t *testing.T) {118 t.Parallel()119 cfg := &config{}120 fileServer := newFileServer(cfg, metrics).init()121 t.Run("HTML files should have Cache-Control: no-cache", func(t *testing.T) {122 w := httptest.NewRecorder()123 r := httptest.NewRequest(http.MethodGet, "/", nil)124 fileServer.Server.Handler.ServeHTTP(w, r)125 last := w.Header().Get("Last-Modified")126 w = httptest.NewRecorder()127 r = httptest.NewRequest(http.MethodGet, "/", nil)128 r.Header.Set("If-Modified-Since", last)129 fileServer.Server.Handler.ServeHTTP(w, r)130 assert.Equal(t, http.StatusNotModified, w.Code)131 assert.Equal(t, w.Header().Get("Cache-Control"), "no-cache")132 })133 t.Run("other files should have Cache-Control: public, max-age=31536000, immutable", func(t *testing.T) {134 w := httptest.NewRecorder()135 r := httptest.NewRequest(http.MethodGet, "/main.8d3db4ef.js", nil)136 fileServer.Server.Handler.ServeHTTP(w, r)137 last := w.Header().Get("Last-Modified")138 w = httptest.NewRecorder()139 r = httptest.NewRequest(http.MethodGet, "/main.8d3db4ef.js", nil)140 r.Header.Set("If-Modified-Since", last)141 fileServer.Server.Handler.ServeHTTP(w, r)142 assert.Equal(t, http.StatusNotModified, w.Code)143 assert.Equal(t, w.Header().Get("Cache-Control"), "public, max-age=31536000, immutable")144 })145 })146}...
serveHTTP
Using AI Code Generation
1import (2func main() {3 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {4 fmt.Fprintln(w, "Hello World")5 })6 http.ListenAndServe(":8080", nil)7}8import (9type hello struct{}10func (h hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {11 fmt.Fprintln(w, "Hello World")12}13func main() {14 http.ListenAndServe(":8080", h)15}16import (17type hello struct{}18func (h hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {19 fmt.Fprintln(w, "Hello World")20}21func main() {22 http.Handle("/", h)23 http.ListenAndServe(":8080", nil)24}25import (26type hello struct{}27func (h hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {28 fmt.Fprintln(w, "Hello World")29}30func main() {31 http.Handle("/hello", h)32 http.ListenAndServe(":8080", nil)33}34import (35type hello struct{}36func (h hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {37 fmt.Fprintln(w, "Hello World")38}39func main() {40 http.Handle("/hello", h)41 http.ListenAndServe(":8080", h)42}43import (44type hello struct{}45func (h hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {46 fmt.Fprintln(w, "Hello World")47}48func main() {49 http.Handle("/hello
serveHTTP
Using AI Code Generation
1import (2func main() {3 http.Handle("/foo", &fooHandler{})4 http.ListenAndServe(":8080", nil)5}6type fooHandler struct{}7func (f *fooHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {8 fmt.Fprintln(w, "Hello, foo!")9}10import (11func main() {12 http.Handle("/foo", &fooHandler{})13 http.ListenAndServe(":8080", nil)14}15type fooHandler struct{}16func (f *fooHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {17 fmt.Fprintln(w, "Hello, foo!")18}19import (20func main() {21 http.HandleFunc("/foo", fooHandler)22 http.ListenAndServe(":8080", nil)23}24func fooHandler(w http.ResponseWriter, r *http.Request) {25 fmt.Fprintln(w, "Hello, foo!")26}27import (28func main() {29 http.HandleFunc("/foo", fooHandler)30 http.ListenAndServe(":8080", nil)31}32func fooHandler(w http.ResponseWriter, r *http.Request) {33 fmt.Fprintln(w, "Hello, foo!")34}35import (36func main() {37 http.HandleFunc("/foo", fooHandler)38 http.ListenAndServe(":8080", nil)39}40func fooHandler(w http.ResponseWriter, r *http.Request) {41 fmt.Fprintln(w, "Hello, foo!")42}43import (44func main() {45 http.HandleFunc("/foo", fooHandler)46 http.ListenAndServe(":8080", nil)47}48func fooHandler(w http.ResponseWriter, r *http.Request) {49 fmt.Fprintln(w, "Hello, foo!")50}51import (
serveHTTP
Using AI Code Generation
1import (2func main() {3 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {4 fmt.Fprintf(w, "Hello World")5 })6 http.ListenAndServe(":8080", nil)7}8import (9func main() {10 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {11 fmt.Fprintf(w, "Hello World")12 })13 log.Fatal(http.ListenAndServe(":8080", nil))14}15import (16func main() {17 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {18 fmt.Fprintf(w, "Hello World")19 })20 log.Fatal(http.ListenAndServe(":8080", nil))21}22import (23func main() {24 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {25 fmt.Fprintf(w, "Hello World")26 })27 log.Fatal(http.ListenAndServe(":8080", nil))28}29import (30func main() {31 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {32 fmt.Fprintf(w, "Hello World")33 })34 log.Fatal(http.ListenAndServe(":8080", nil))35}36import (37func main() {38 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {39 fmt.Fprintf(w, "Hello World")40 })41 log.Fatal(http.ListenAndServe(":8080", nil))42}43import (44func main() {45 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {46 fmt.Fprintf(w, "Hello World")47 })48 log.Fatal(http.ListenAndServe
serveHTTP
Using AI Code Generation
1func main() {2 http.HandleFunc("/", handler)3 http.ListenAndServe(":8080", nil)4}5func main() {6 http.HandleFunc("/", handler)7 http.ListenAndServe(":8080", nil)8}9func main() {10 http.HandleFunc("/", handler)11 http.ListenAndServe(":8080", nil)12}13func main() {14 http.HandleFunc("/", handler)15 http.ListenAndServe(":8080", nil)16}17func main() {18 http.HandleFunc("/", handler)19 http.ListenAndServe(":8080", nil)20}21func main() {22 http.HandleFunc("/", handler)23 http.ListenAndServe(":8080", nil)24}25func main() {26 http.HandleFunc("/", handler)27 http.ListenAndServe(":8080", nil)28}29func main() {30 http.HandleFunc("/", handler)31 http.ListenAndServe(":8080", nil)32}33func main() {34 http.HandleFunc("/", handler)35 http.ListenAndServe(":8080", nil)36}
serveHTTP
Using AI Code Generation
1func main() {2 http.Handle("/", &main{})3 http.ListenAndServe(":8080", nil)4}5func main() {6 http.Handle("/", &main{})7 http.ListenAndServe(":8080", nil)8}9func main() {10 http.Handle("/", &main{})11 http.ListenAndServe(":8080", nil)12}13func main() {14 http.Handle("/", &main{})15 http.ListenAndServe(":8080", nil)16}17func main() {18 http.Handle("/", &main{})19 http.ListenAndServe(":8080", nil)20}21func main() {22 http.Handle("/", &main{})23 http.ListenAndServe(":8080", nil)24}25func main() {26 http.Handle("/", &main{})27 http.ListenAndServe(":8080", nil)28}29func main() {30 http.Handle("/", &main{})31 http.ListenAndServe(":8080", nil)32}33func main() {34 http.Handle("/", &main{})35 http.ListenAndServe(":8080", nil)36}37func main() {38 http.Handle("/", &main{})39 http.ListenAndServe(":8080", nil)40}41func main() {42 http.Handle("/", &main{})43 http.ListenAndServe(":8080", nil)44}45func main() {46 http.Handle("/", &main{})47 http.ListenAndServe(":8080", nil)48}49func main() {50 http.Handle("/", &main{})51 http.ListenAndServe(":8080
serveHTTP
Using AI Code Generation
1import (2func main() {3 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {4 fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])5 })6 log.Fatal(http.ListenAndServe(":8080", nil))7}8import (9type Hello struct{}10func (h Hello) ServeHTTP(w http.ResponseWriter, r *http.Request) {11 fmt.Fprint(w, "Hello!")12}13func main() {14 err := http.ListenAndServe("localhost:4000", h)15 if err != nil {16 log.Fatal(err)17 }18}19import (20type Struct struct {21}22func (s String) ServeHTTP(w http.ResponseWriter, r *http.Request) {23 fmt.Fprint(w, s)24}25func (s *Struct) ServeHTTP(w http.ResponseWriter, r *http.Request) {26 fmt.Fprint(w, s.Greeting, s.Punct, s.Who)27}28func main() {29 http.Handle("/string", String("I'm a frayed knot."))30 http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})31 log.Fatal(http.ListenAndServe("localhost:4000", nil))32}33import (34func main() {35 http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {36 fmt.Fprint(w, "Hello, ")37 })38 http.HandleFunc("/world", func(w http.ResponseWriter, r *http.Request) {39 fmt.Fprint(w, "World!")
serveHTTP
Using AI Code Generation
1func main() {2http.Handle("/hello", &main{})3http.ListenAndServe(":8080", nil)4}5func main() {6http.Handle("/hello", &main{})7http.ListenAndServe(":8080", nil)8}9func main() {10http.Handle("/hello", &main{})11http.ListenAndServe(":8080", nil)12}13func main() {14http.Handle("/hello", &main{})15http.ListenAndServe(":8080", nil)16}17func main() {18http.Handle("/hello", &main{})19http.ListenAndServe(":8080", nil)20}21func main() {22http.Handle("/hello", &main{})23http.ListenAndServe(":8080", nil)24}25func main() {26http.Handle("/hello", &main{})27http.ListenAndServe(":8080", nil)28}29func main() {30http.Handle("/hello", &main{})31http.ListenAndServe(":8080", nil)32}33func main() {34http.Handle("/hello", &main{})35http.ListenAndServe(":8080", nil)36}37func main() {38http.Handle("/hello", &main{})39http.ListenAndServe(":8080", nil)40}41func main() {42http.Handle("/hello", &main{})43http.ListenAndServe(":8080", nil)44}45func main() {46http.Handle("/hello", &main{})47http.ListenAndServe(":8080", nil)48}49func main() {50http.Handle("/hello", &main{})51http.ListenAndServe(":8080
serveHTTP
Using AI Code Generation
1func main() {2 http.Handle("/", new(main))3 http.ListenAndServe(":5000", nil)4}5func main() {6 http.Handle("/", new(main))7 http.ListenAndServe(":5000", nil)8}9func main() {10 http.Handle("/", new(main))11 http.ListenAndServe(":5000", nil)12}13func main() {14 http.Handle("/", new(main))15 http.ListenAndServe(":5000", nil)16}17func main() {18 http.Handle("/", new(main))19 http.ListenAndServe(":5000", nil)20}21func main() {22 http.Handle("/", new(main))23 http.ListenAndServe(":5000", nil)24}25func main() {26 http.Handle("/", new(main))27 http.ListenAndServe(":5000", nil)28}29func main() {30 http.Handle("/", new(main))31 http.ListenAndServe(":5000", nil)32}33func main() {34 http.Handle("/", new(main))35 http.ListenAndServe(":5000", nil)36}37func main() {38 http.Handle("/", new(main))39 http.ListenAndServe(":5000", nil)40}41func main() {42 http.Handle("/", new(main))43 http.ListenAndServe(":5000", nil)44}45func main() {46 http.Handle("/", new(main))47 http.ListenAndServe(":5000", nil)48}49func main() {50 http.Handle("/", new(main))51 http.ListenAndServe(":5000
serveHTTP
Using AI Code Generation
1import (2func main() {3 http.Handle("/test", &myHandler{})4 http.ListenAndServe(":8080", nil)5}6import (7type myHandler struct{}8func (m *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {9 fmt.Fprintf(w, "Hello World!")10}11func main() {12 http.ListenAndServe(":8080", &myHandler{})13}14Your name to display (optional):15Your name to display (optional):
serveHTTP
Using AI Code Generation
1import (2func main() {3 http.HandleFunc("/hello", hello)4 http.HandleFunc("/world", world)5 http.ListenAndServe(":8080", nil)6}7func hello(w http.ResponseWriter, r *http.Request) {8 fmt.Fprintf(w, "Hello")9}10func world(w http.ResponseWriter, r *http.Request) {11 fmt.Fprintf(w, "World")12}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!