diff --git a/Makefile b/Makefile index 8a479de..bcd8850 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ ELM_FILES = $(shell find . -type f -name '*.elm') NODE_BIN = ./node_modules/.bin ELM-MAKE = $(NODE_BIN)/elm-make ELM-REACTOR = $(NODE_BIN)/elm-reactor +ELM-TEST = $(NODE_BIN)/elm-test ELMMAKE_FLAGS = --yes ifeq ($(DEBUG),1) @@ -31,3 +32,6 @@ clean: run: all $(ELM-REACTOR) + +test: + $(ELM-TEST) diff --git a/package.json b/package.json index 0e57575..5ee6d44 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "elm-pass", "version": "1.0.0", "dependencies": { - "elm": "0.18" + "elm": "0.18", + "elm-test": "0.18.12" } } diff --git a/src/Gitlab.elm b/src/Gitlab.elm index d5bbbe3..f3a039d 100644 --- a/src/Gitlab.elm +++ b/src/Gitlab.elm @@ -44,16 +44,11 @@ request : String -> Path -> Decoder a -> Client -> Http.Request a request method path decoder client = let url = - String.join "/" <| - client.url - :: "api/v4" - :: path + apiUrl client path in Http.request { method = method - , headers = - Maybe.Extra.values - [ Maybe.map (Http.header "Private-Token") client.token ] + , headers = apiHeaders client , url = url , body = Http.emptyBody , expect = Http.expectJson decoder @@ -62,6 +57,20 @@ request method path decoder client = } +apiUrl : Client -> Path -> String +apiUrl client path = + String.join "/" <| + client.url + :: "api/v4" + :: path + + +apiHeaders : Client -> List Http.Header +apiHeaders client = + Maybe.Extra.values + [ Maybe.map (Http.header "Private-Token") client.token ] + + get : Path -> Decoder a -> Client -> Http.Request a get = request "GET" diff --git a/tests/GitlabTests.elm b/tests/GitlabTests.elm new file mode 100644 index 0000000..fa07410 --- /dev/null +++ b/tests/GitlabTests.elm @@ -0,0 +1,33 @@ +module GitlabTests exposing (..) + +import Expect +import Http +import Gitlab +import Test exposing (..) + + +suite : Test +suite = + let + clientWithoutToken = + Gitlab.Client "http://example.org" Nothing + + clientWithToken = + Gitlab.Client "http://example.org" (Just "token") + in + describe "Gitlab" + [ test "Builds v4 API url" <| + \() -> + Expect.equal "http://example.org/api/v4/foo/bar" <| + Gitlab.apiUrl clientWithToken [ "foo", "bar" ] + , test "Includes private token in headers when available" <| + \() -> + Expect.true "Expected token header." <| + List.member (Http.header "Private-Token" "token") <| + Gitlab.apiHeaders clientWithToken + , test "Omits private token in headers when not available" <| + \() -> + Expect.false "Did not expect token header." <| + List.member (Http.header "Private-Token" "token") <| + Gitlab.apiHeaders clientWithoutToken + ] diff --git a/tests/elm-package.json b/tests/elm-package.json new file mode 100644 index 0000000..826702f --- /dev/null +++ b/tests/elm-package.json @@ -0,0 +1,22 @@ +{ + "version": "1.0.0", + "summary": "Test Suites", + "repository": "https://github.com/user/project.git", + "license": "BSD3", + "source-directories": [ + "../src", + "." + ], + "exposed-modules": [], + "dependencies": { + "NoRedInk/elm-decode-pipeline": "3.0.0 <= v < 4.0.0", + "eeue56/elm-html-test": "5.1.2 <= v < 6.0.0", + "elm-community/elm-test": "4.0.0 <= v < 5.0.0", + "elm-community/maybe-extra": "4.0.0 <= v < 5.0.0", + "elm-lang/core": "5.1.1 <= v < 6.0.0", + "elm-lang/html": "2.0.0 <= v < 3.0.0", + "elm-lang/http": "1.0.0 <= v < 2.0.0", + "krisajenkins/remotedata": "4.3.3 <= v < 5.0.0" + }, + "elm-version": "0.18.0 <= v < 0.19.0" +}