This is an automated email from the ASF dual-hosted git repository.

houqp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airflow-client-go.git


The following commit(s) were added to refs/heads/master by this push:
     new e1deffd  add test & doc for authentication (#5)
e1deffd is described below

commit e1deffdac6529e02da148888d530ba3099644187
Author: QP Hou <[email protected]>
AuthorDate: Sat Sep 5 22:31:02 2020 -0700

    add test & doc for authentication (#5)
---
 .github/workflows/ci.yml | 14 +++++++---
 README.md                |  8 +++++-
 client_test.go           | 70 ++++++++++++++++++++++++++++++++++++++++++++++++
 go.mod                   |  8 ++++++
 4 files changed, 96 insertions(+), 4 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index d45816c..63714d5 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -39,6 +39,9 @@ jobs:
 
   build:
     runs-on: ubuntu-latest
+    strategy:
+      matrix:
+        go_version: [1.13, 1.14, 1.15]
     steps:
       - name: Checkout
         uses: actions/checkout@master
@@ -46,8 +49,13 @@ jobs:
       - name: Set up Go
         uses: actions/setup-go@v1
         with:
-          go-version: 1.14
+          go-version: ${{ matrix.go_version }}
         id: go
 
-      - name: build & test
-        run: cd airflow && go test
+      - name: Run tests
+        run: |
+            go test
+
+      - name: Run auto generated tests
+        run: |
+            cd airflow && go test
diff --git a/README.md b/README.md
index 12f43c7..c89d776 100644
--- a/README.md
+++ b/README.md
@@ -37,7 +37,13 @@ func main() {
                Host:     "localhost:28080",
                BasePath: "/api/v1",
        })
-       ctx := context.TODO()
+
+       cred := airflow.BasicAuth{
+               UserName: "username",
+               Password: "password",
+       }
+       ctx := context.WithValue(context.Background(), 
airflow.ContextBasicAuth, cred)
+
        variable, _, err := client.VariableApi.GetVariable(ctx, "foo")
        if err != nil {
                fmt.Println(variable)
diff --git a/client_test.go b/client_test.go
new file mode 100644
index 0000000..9db3b25
--- /dev/null
+++ b/client_test.go
@@ -0,0 +1,70 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package client_test
+
+import (
+       "context"
+       "encoding/base64"
+       "fmt"
+       "net/http"
+       "net/http/httptest"
+       "net/url"
+       "testing"
+
+       "github.com/apache/airflow-client-go/airflow"
+       "github.com/stretchr/testify/assert"
+)
+
+func TestBasicAuth(t *testing.T) {
+       // Start a local HTTP server
+       server := httptest.NewServer(http.HandlerFunc(func(rw 
http.ResponseWriter, req *http.Request) {
+               assert.Equal(
+                       t,
+                       fmt.Sprintf(
+                               "Basic %s",
+                               
base64.StdEncoding.EncodeToString([]byte("username:password")),
+                       ),
+                       req.Header.Get("Authorization"))
+               assert.Equal(t, "/api/v1/variables/foo", req.URL.String())
+               rw.Header().Set("Content-Type", "application/json")
+               rw.Write([]byte(`{"key": "foo", "value": "bar"}`))
+       }))
+       // Close the server when test finishes
+       defer server.Close()
+       url, err := url.Parse(server.URL)
+       assert.Equal(t, nil, err)
+
+       cli := airflow.NewAPIClient(&airflow.Configuration{
+               Scheme:   "http",
+               Host:     url.Host,
+               BasePath: "/api/v1",
+       })
+
+       cred := airflow.BasicAuth{
+               UserName: "username",
+               Password: "password",
+       }
+       ctx := context.WithValue(context.Background(), 
airflow.ContextBasicAuth, cred)
+
+       variable, _, err := cli.VariableApi.GetVariable(ctx, "foo")
+       assert.Equal(t, nil, err)
+       assert.Equal(t, airflow.Variable{
+               Key:   "foo",
+               Value: "bar",
+       }, variable)
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..05f0db3
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,8 @@
+module github.com/apache/airflow-client-go
+
+go 1.13
+
+require (
+       github.com/apache/airflow-client-go/airflow 
v0.0.0-20200725194829-781c285536c1
+       github.com/stretchr/testify v1.6.1
+)

Reply via email to