From cb8e2a5ae27aa34f2a9ad21469538403274e3b3e Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Thu, 4 Jul 2024 15:27:10 +0200 Subject: Discovery: Implement conditional requests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From docs.eduvpn.org (https://docs.eduvpn.org/server/v3/server-discovery.html): When fetching the new JSON files, the client SHOULD use conditional requests, so the file is only fetched in case there were changes. This, in order to reduce the amount of traffic required by VPN clients, especially relevant for metered, or slow connections. The If-Modified-Since request header SHOULD be used for this. If the response code is 304, the file hasn’t changed since. The value to use for the If-Modified-Since request header is the exact value of the Last-Modified response header for the last successful download, i.e. 200 response. You SHOULD store the value of Last-Modified and use it as-is for the If-Modified-Since header. --- internal/discovery/discovery_test.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'internal/discovery/discovery_test.go') diff --git a/internal/discovery/discovery_test.go b/internal/discovery/discovery_test.go index 1101bb2..5672b9f 100644 --- a/internal/discovery/discovery_test.go +++ b/internal/discovery/discovery_test.go @@ -50,12 +50,22 @@ func TestServers(t *testing.T) { }, SupportContact: []string{"mailto:test@example.org"}, } + // conditional requests: this should not be fetched fresh + _, fresh, err = d.Servers(context.Background()) + if fresh { + t.Fatalf("Obtained the server list fresh with conditional requests") + } + if err != nil { + t.Fatalf("Failed getting servers after inserting a mock entry: %v", err) + } + // mock conditional requests + d.ServerList.UpdateHeader = time.Time{} s1, fresh, err := d.Servers(context.Background()) if !fresh { - t.Fatalf("Did not obtain the server list fresh after inserting a mock entry and faking expiry") + t.Fatalf("Did not obtain the server list fresh with mocked conditional request and mocked entry") } if err != nil { - t.Fatalf("Failed getting servers after inserting a mock entry: %v", err) + t.Fatalf("Failed getting servers after inserting a mock entry and mocking conditional request: %v", err) } if kws := s1.List[len(s1.List)-1].KeywordList; kws != nil { t.Fatalf("KeywordList is not nil when getting a fresh server list after inserting a mock entry: %v", kws) @@ -134,12 +144,22 @@ func TestOrganizations(t *testing.T) { "en": "test bla", }, } + + _, fresh, err = d.Organizations(context.Background()) + if fresh { + t.Fatalf("Obtained the organization list fresh with conditional requests") + } + if err != nil { + t.Fatalf("Failed getting organizations after inserting a mock entry: %v", err) + } + // mock conditional requests + d.OrganizationList.UpdateHeader = time.Time{} s1, fresh, err := d.Organizations(context.Background()) if !fresh { - t.Fatalf("Did not obtain the organization list fresh after inserting a mock entry and faking expiry") + t.Fatalf("Did not obtain the organization list fresh after inserting a mock entry, faking expiry and mocking conditional request") } if err != nil { - t.Fatalf("Failed getting organizations after inserting a mock entry: %v", err) + t.Fatalf("Failed getting organizations after inserting a mock entry and faking conditional request: %v", err) } if kws := s1.List[len(s1.List)-1].KeywordList; kws != nil { t.Fatalf("KeywordList is not nil when getting a fresh organization list after inserting a mock entry: %v", kws) -- cgit v1.2.3