diff --git a/app/main.go b/app/main.go index 4c7cff7e..a9ff038d 100644 --- a/app/main.go +++ b/app/main.go @@ -126,7 +126,7 @@ func main() { }.Do() } - srv.Run() + srv.Run(8080) } // makeBoltStore creates store for all sites diff --git a/app/rest/server.go b/app/rest/server.go index 007af8aa..1b577872 100644 --- a/app/rest/server.go +++ b/app/rest/server.go @@ -3,6 +3,7 @@ package rest import ( "bytes" "encoding/json" + "fmt" "log" "net/http" "path/filepath" @@ -42,7 +43,7 @@ type Server struct { } // Run the lister and request's router, activate rest server -func (s *Server) Run() { +func (s *Server) Run(port int) { log.Print("[INFO] activate rest server") // add auth.Developer flag if dev mode is active @@ -105,7 +106,7 @@ func (s *Server) Run() { }) s.addFileServer(router, "/web", http.Dir(filepath.Join(".", "web"))) - s.httpServer = &http.Server{Addr: ":8080", Handler: router} + s.httpServer = &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: router} err := s.httpServer.ListenAndServe() log.Printf("[WARN] http server terminated, %s", err) } diff --git a/app/rest/server_test.go b/app/rest/server_test.go index 2da892b2..f14b8963 100644 --- a/app/rest/server_test.go +++ b/app/rest/server_test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "math/rand" "net/http" "os" "strings" @@ -21,22 +22,22 @@ import ( var testDb = "/tmp/test-remark.db" func TestServer_Ping(t *testing.T) { - srv := prep(t) + srv, port := prep(t) assert.NotNil(t, srv) defer cleanup(srv) - res, code := get(t, "http://127.0.0.1:8080/api/v1/ping") + res, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/ping", port)) assert.Equal(t, "pong", res) assert.Equal(t, 200, code) } func TestServer_Create(t *testing.T) { - srv := prep(t) + srv, port := prep(t) assert.NotNil(t, srv) defer cleanup(srv) r := strings.NewReader(`{"text": "test 123", "locator":{"url": "https://radio-t.com/blah1", "site": "radio-t"}}`) - resp, err := http.Post("http://127.0.0.1:8080/api/v1/comment", "application/json", r) + resp, err := http.Post(fmt.Sprintf("http://127.0.0.1:%d/api/v1/comment", port), "application/json", r) assert.Nil(t, err) assert.Equal(t, http.StatusCreated, resp.StatusCode) @@ -52,13 +53,13 @@ func TestServer_Create(t *testing.T) { } func TestServer_CreateAndGet(t *testing.T) { - srv := prep(t) + srv, port := prep(t) assert.NotNil(t, srv) defer cleanup(srv) // create comment r := strings.NewReader(`{"text": "test 123", "locator":{"url": "https://radio-t.com/blah1", "site": "radio-t"}}`) - resp, err := http.Post("http://127.0.0.1:8080/api/v1/comment", "application/json", r) + resp, err := http.Post(fmt.Sprintf("http://127.0.0.1:%d/api/v1/comment", port), "application/json", r) assert.Nil(t, err) assert.Equal(t, http.StatusCreated, resp.StatusCode) b, err := ioutil.ReadAll(resp.Body) @@ -70,7 +71,7 @@ func TestServer_CreateAndGet(t *testing.T) { id := c["id"].(string) // get created comment by id - res, code := get(t, fmt.Sprintf("http://127.0.0.1:8080/api/v1/id/%s?site=radio-t&url=https://radio-t.com/blah1", id)) + res, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/id/%s?site=radio-t&url=https://radio-t.com/blah1", port, id)) assert.Equal(t, 200, code) comment := store.Comment{} err = json.Unmarshal([]byte(res), &comment) @@ -83,11 +84,11 @@ func TestServer_CreateAndGet(t *testing.T) { } func TestServer_Find(t *testing.T) { - srv := prep(t) + srv, port := prep(t) assert.NotNil(t, srv) defer cleanup(srv) - _, code := get(t, "http://127.0.0.1:8080/api/v1/find?site=radio-t&url=https://radio-t.com/blah1") + _, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/find?site=radio-t&url=https://radio-t.com/blah1", port)) assert.Equal(t, 400, code, "nothing in") c1 := store.Comment{Text: "test test #1", ParentID: "p1", @@ -95,12 +96,12 @@ func TestServer_Find(t *testing.T) { c2 := store.Comment{Text: "test test #2", ParentID: "p1", Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah1"}} - id1 := addComment(t, c1) - id2 := addComment(t, c2) + id1 := addComment(t, c1, port) + id2 := addComment(t, c2, port) assert.NotEqual(t, id1, id2) // get sorted by +time - res, code := get(t, "http://127.0.0.1:8080/api/v1/find?site=radio-t&url=https://radio-t.com/blah1&sort=+time") + res, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/find?site=radio-t&url=https://radio-t.com/blah1&sort=+time", port)) assert.Equal(t, 200, code) comments := []store.Comment{} err := json.Unmarshal([]byte(res), &comments) @@ -110,7 +111,7 @@ func TestServer_Find(t *testing.T) { assert.Equal(t, id2, comments[1].ID) // get sorted by -time - res, code = get(t, "http://127.0.0.1:8080/api/v1/find?site=radio-t&url=https://radio-t.com/blah1&sort=-time") + res, code = get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/find?site=radio-t&url=https://radio-t.com/blah1&sort=-time", port)) assert.Equal(t, 200, code) err = json.Unmarshal([]byte(res), &comments) assert.Nil(t, err) @@ -120,17 +121,17 @@ func TestServer_Find(t *testing.T) { } func TestServer_Update(t *testing.T) { - srv := prep(t) + srv, port := prep(t) assert.NotNil(t, srv) defer cleanup(srv) c1 := store.Comment{Text: "test test #1", ParentID: "p1", Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah1"}} - id := addComment(t, c1) + id := addComment(t, c1, port) client := http.Client{} req, err := http.NewRequest(http.MethodPut, - "http://127.0.0.1:8080/api/v1/comment/"+id+"?site=radio-t&url=https://radio-t.com/blah1", + fmt.Sprintf("http://127.0.0.1:%d/api/v1/comment/"+id+"?site=radio-t&url=https://radio-t.com/blah1", port), strings.NewReader(`{"text":"updated text", "summary":"my edit"}`)) assert.Nil(t, err) b, err := client.Do(req) @@ -149,7 +150,7 @@ func TestServer_Update(t *testing.T) { assert.True(t, time.Since(c2.Edit.Timestamp) < 1*time.Second) // read updated comment - res, code := get(t, fmt.Sprintf("http://127.0.0.1:8080/api/v1/id/%s?site=radio-t&url=https://radio-t.com/blah1", id)) + res, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/id/%s?site=radio-t&url=https://radio-t.com/blah1", port, id)) assert.Equal(t, 200, code) c3 := store.Comment{} err = json.Unmarshal([]byte(res), &c3) @@ -158,7 +159,7 @@ func TestServer_Update(t *testing.T) { } func TestServer_Last(t *testing.T) { - srv := prep(t) + srv, port := prep(t) assert.NotNil(t, srv) defer cleanup(srv) @@ -168,11 +169,11 @@ func TestServer_Last(t *testing.T) { Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah2"}} // add 3 comments - addComment(t, c1) - id1 := addComment(t, c1) - id2 := addComment(t, c2) + addComment(t, c1, port) + id1 := addComment(t, c1, port) + id2 := addComment(t, c2, port) - res, code := get(t, "http://127.0.0.1:8080/api/v1/last/2?site=radio-t") + res, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/last/2?site=radio-t", port)) assert.Equal(t, 200, code) comments := []store.Comment{} err := json.Unmarshal([]byte(res), &comments) @@ -181,7 +182,7 @@ func TestServer_Last(t *testing.T) { assert.Equal(t, id1, comments[1].ID) assert.Equal(t, id2, comments[0].ID) - res, code = get(t, "http://127.0.0.1:8080/api/v1/last/5?site=radio-t") + res, code = get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/last/5?site=radio-t", port)) assert.Equal(t, 200, code) err = json.Unmarshal([]byte(res), &comments) assert.Nil(t, err) @@ -189,7 +190,7 @@ func TestServer_Last(t *testing.T) { } func TestServer_FindUserComments(t *testing.T) { - srv := prep(t) + srv, port := prep(t) assert.NotNil(t, srv) defer cleanup(srv) @@ -199,14 +200,14 @@ func TestServer_FindUserComments(t *testing.T) { Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah2"}} // add 3 comments - addComment(t, c1) - addComment(t, c2) - addComment(t, c2) + addComment(t, c1, port) + addComment(t, c2, port) + addComment(t, c2, port) - _, code := get(t, "http://127.0.0.1:8080/api/v1/comments?site=radio-t&user=blah") + _, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/comments?site=radio-t&user=blah", port)) assert.Equal(t, 400, code, "noting for user blah") - res, code := get(t, "http://127.0.0.1:8080/api/v1/comments?site=radio-t&user=dev") + res, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/comments?site=radio-t&user=dev", port)) assert.Equal(t, 200, code) comments := []store.Comment{} err := json.Unmarshal([]byte(res), &comments) @@ -214,10 +215,10 @@ func TestServer_FindUserComments(t *testing.T) { assert.Equal(t, 3, len(comments), "should have 3 comments") } -func prep(t *testing.T) *Server { +func prep(t *testing.T) (srv *Server, port int) { dataStore, err := store.NewBoltDB(store.BoltSite{FileName: testDb, SiteID: "radio-t"}) assert.Nil(t, err) - srv := Server{ + srv = &Server{ DataService: store.Service{Interface: dataStore, EditDuration: 5 * time.Minute}, DevMode: true, AuthFacebook: &auth.Provider{}, @@ -225,10 +226,11 @@ func prep(t *testing.T) *Server { AuthGoogle: &auth.Provider{}, } go func() { - srv.Run() + port = rand.Intn(50000) + 1025 + srv.Run(port) }() time.Sleep(100 * time.Millisecond) - return &srv + return srv, port } func get(t *testing.T, url string) (string, int) { @@ -240,11 +242,11 @@ func get(t *testing.T, url string) (string, int) { return string(body), r.StatusCode } -func addComment(t *testing.T, c store.Comment) string { +func addComment(t *testing.T, c store.Comment, port int) string { b, err := json.Marshal(c) assert.Nil(t, err, "can't marshal comment %+v", c) - resp, err := http.Post("http://127.0.0.1:8080/api/v1/comment", "application/json", bytes.NewBuffer(b)) + resp, err := http.Post(fmt.Sprintf("http://127.0.0.1:%d/api/v1/comment", port), "application/json", bytes.NewBuffer(b)) assert.Nil(t, err) assert.Equal(t, http.StatusCreated, resp.StatusCode) b, err = ioutil.ReadAll(resp.Body)