11package main
22
33import (
4+ "bytes"
45 "encoding/json"
56 "net/http"
67 "os"
@@ -10,16 +11,16 @@ import (
1011 "example.com/tes"
1112)
1213
13- func readExampleTask (filename string ) tes.TesTask {
14+ func readExampleTask (filename string ) tes.Task {
1415 data , err := os .ReadFile (filename )
1516 if err != nil {
1617 panic (err )
1718 }
1819
19- var task tes.TesTask
20+ var task tes.Task
2021 err = json .Unmarshal (data , & task )
2122 if err != nil {
22- return tes.TesTask {}
23+ return tes.Task {}
2324 }
2425
2526 return task
@@ -31,28 +32,35 @@ func TestValidUser(t *testing.T) {
3132
3233 // Read example task
3334 task := readExampleTask ("../example-tasks/hello-world.json" )
35+ taskData , _ := json .Marshal (task )
3436
3537 // Create an Authorization Header to pass to the authorizer
36- authHeader := http.Header {
37- "Authorization" : []string {"Bearer Alyssa P. Hacker" },
38- }
39-
40- authenticator := ExampleAuthorizer {}
41- resp , err := authenticator .Authorize (authHeader , task )
38+ req , _ := http .NewRequest ("POST" , "http://localhost:8080/s3" , bytes .NewBuffer (taskData ))
39+ req .Header .Set ("Authorization" , "Bearer Alyssa P. Hacker" )
40+ client := & http.Client {}
41+ resp , err := client .Do (req )
4242
4343 if err != nil {
4444 t .Fatalf ("Expected no error, got %v" , err )
4545 }
46+ defer resp .Body .Close ()
47+
48+ if resp .StatusCode != http .StatusOK {
49+ t .Errorf ("Expected status code 200, got %v" , resp .StatusCode )
50+ }
51+
52+ var authResp auth.Auth
53+ json .NewDecoder (resp .Body ).Decode (& authResp )
4654
4755 expected := auth.Auth {
4856 User : "Alyssa P. Hacker" ,
4957 Token : "<Alyssa's Secret>" ,
5058 }
5159
52- if resp .User != expected .User || resp .Token != expected .Token {
60+ if authResp .User != expected .User || authResp .Token != expected .Token {
5361 t .Errorf ("Expected (%s, %s), got (%s, %s)" ,
5462 expected .User , expected .Token ,
55- resp .User , resp .Token )
63+ authResp .User , authResp .Token )
5664 }
5765}
5866
@@ -62,17 +70,21 @@ func TestInvalidUser(t *testing.T) {
6270
6371 // Read example task
6472 task := readExampleTask ("../example-tasks/hello-world.json" )
73+ taskData , _ := json .Marshal (task )
6574
6675 // Create an Authorization Header to pass to the authorizer
67- authHeader := http.Header {
68- "Authorization" : []string {"Bearer Foo" },
69- }
76+ req , _ := http .NewRequest ("POST" , "http://localhost:8080/s3" , bytes .NewBuffer (taskData ))
77+ req .Header .Set ("Authorization" , "Bearer Foo" )
78+ client := & http.Client {}
79+ resp , err := client .Do (req )
7080
71- authenticator := ExampleAuthorizer {}
72- _ , err := authenticator .Authorize (authHeader , task )
81+ if err != nil {
82+ t .Fatalf ("Expected no error, got %v" , err )
83+ }
84+ defer resp .Body .Close ()
7385
74- if err == nil {
75- t .Fatalf ("Expected 401 Unauthorized error , got %v" , err )
86+ if resp . StatusCode != http . StatusUnauthorized {
87+ t .Errorf ("Expected status code 401 , got %v" , resp . StatusCode )
7688 }
7789}
7890
@@ -82,17 +94,21 @@ func TestInvalidAuthHeader(t *testing.T) {
8294
8395 // Read example task
8496 task := readExampleTask ("../example-tasks/hello-world.json" )
97+ taskData , _ := json .Marshal (task )
8598
8699 // Create an Authorization Header to pass to the authorizer
87- authHeader := http.Header {
88- "Authorization" : []string {"Basic Alyssa P. Hacker" },
89- }
100+ req , _ := http .NewRequest ("POST" , "http://localhost:8080/s3" , bytes .NewBuffer (taskData ))
101+ req .Header .Set ("Authorization" , "Basic Alyssa P. Hacker" )
102+ client := & http.Client {}
103+ resp , err := client .Do (req )
90104
91- authenticator := ExampleAuthorizer {}
92- _ , err := authenticator .Authorize (authHeader , task )
105+ if err != nil {
106+ t .Fatalf ("Expected no error, got %v" , err )
107+ }
108+ defer resp .Body .Close ()
93109
94- if err == nil {
95- t .Fatalf ("Expected 401 Unauthorized error , got %v" , err )
110+ if resp . StatusCode != http . StatusUnauthorized {
111+ t .Errorf ("Expected status code 401 , got %v" , resp . StatusCode )
96112 }
97113}
98114
@@ -102,14 +118,19 @@ func TestMissingAuthHeader(t *testing.T) {
102118
103119 // Read example task
104120 task := readExampleTask ("../example-tasks/hello-world.json" )
121+ taskData , _ := json .Marshal (task )
105122
106123 // Create an Authorization Header to pass to the authorizer
107- authHeader := http.Header {}
124+ req , _ := http .NewRequest ("POST" , "http://localhost:8080/s3" , bytes .NewBuffer (taskData ))
125+ client := & http.Client {}
126+ resp , err := client .Do (req )
108127
109- authenticator := ExampleAuthorizer {}
110- _ , err := authenticator .Authorize (authHeader , task )
128+ if err != nil {
129+ t .Fatalf ("Expected no error, got %v" , err )
130+ }
131+ defer resp .Body .Close ()
111132
112- if err == nil {
113- t .Fatalf ("Expected 401 Unauthorized error , got %v" , err )
133+ if resp . StatusCode != http . StatusUnauthorized {
134+ t .Errorf ("Expected status code 401 , got %v" , resp . StatusCode )
114135 }
115136}
0 commit comments