-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into rf/linked-pages
- Loading branch information
Showing
8 changed files
with
156 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
type Headers struct { | ||
client *http.Client | ||
} | ||
|
||
func NewHeaders(client *http.Client) *Headers { | ||
return &Headers{client: client} | ||
} | ||
|
||
func (h *Headers) List(ctx context.Context, url string) (map[string]string, error) { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := h.client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
responseHeaders := make(map[string]string) | ||
for k, v := range resp.Header { | ||
for _, s := range v { | ||
responseHeaders[k] = s | ||
} | ||
} | ||
|
||
return responseHeaders, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package checks | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/xray-web/web-check-api/testutils" | ||
) | ||
|
||
func TestList(t *testing.T) { | ||
t.Parallel() | ||
|
||
c := testutils.MockClient(&http.Response{ | ||
Header: http.Header{ | ||
"Cache-Control": {"private, max-age=0"}, | ||
"X-Xss-Protection": {"0"}, | ||
}, | ||
}) | ||
h := NewHeaders(c) | ||
|
||
actual, err := h.List(context.Background(), "example.com") | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, "private, max-age=0", actual["Cache-Control"]) | ||
assert.Equal(t, "0", actual["X-Xss-Protection"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/xray-web/web-check-api/config" | ||
"github.com/xray-web/web-check-api/server" | ||
) | ||
|
||
func main() { | ||
s := server.New(config.New()) | ||
log.Println(s.Run()) | ||
srv := server.New(config.New()) | ||
|
||
done := make(chan os.Signal, 1) | ||
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | ||
|
||
go func() { | ||
if err := srv.Run(); err != nil && err != http.ErrServerClosed { | ||
log.Fatalf("listen: %v\n", err) | ||
} | ||
}() | ||
|
||
<-done | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) | ||
defer func() { | ||
// extra handling here, databases etc | ||
cancel() | ||
}() | ||
|
||
if err := srv.Shutdown(ctx); err != nil { | ||
log.Fatalf("Server Shutdown Failed:%+v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package server | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/xray-web/web-check-api/config" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
func TestServer(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("start server", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
srv := New(config.New()) | ||
srv.routes() | ||
ts := httptest.NewServer(srv.CORS(srv.mux)) | ||
defer ts.Close() | ||
|
||
// wait up tot 10 seconds for health check to return 200 | ||
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(+10*time.Second)) | ||
defer cancel() | ||
for { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/health", nil) | ||
assert.NoError(t, err) | ||
resp, err := http.DefaultClient.Do(req) | ||
if err == nil && resp.StatusCode == http.StatusOK { | ||
break | ||
} | ||
} | ||
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
err := srv.Shutdown(ctx) | ||
assert.NoError(t, err) | ||
}) | ||
} |