@@ -4,10 +4,13 @@ import (
44 "bytes"
55 "context"
66 "io"
7+ "net/http"
8+ "net/http/httptest"
79 "os"
810 "path/filepath"
911 "testing"
1012
13+ "github.com/spf13/viper"
1114 "github.com/stretchr/testify/assert"
1215 "github.com/stretchr/testify/require"
1316)
@@ -300,3 +303,57 @@ func TestGetIPRanges_List(t *testing.T) {
300303 })
301304 assert .Equal (t , "ActionGroup\n AzureStorage\n " , out )
302305}
306+
307+ // Regression: azure's two-stage hosted fetch (HTML scrape -> JSON download)
308+ // previously bypassed the cache because both steps reduced to raw URL calls
309+ // that GetRawData doesn't cache. Verify the cache wrap now keys the resolved
310+ // JSON under "azure" so a second call within TTL returns from disk.
311+ func TestFetchRawData_HostedCachesUnderAzureKey (t * testing.T ) {
312+ t .Setenv ("XDG_CACHE_HOME" , t .TempDir ())
313+ t .Cleanup (func () { viper .Reset () })
314+
315+ jsonBody := loadFixture (t )
316+
317+ var hits int
318+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
319+ hits ++
320+ _ , _ = w .Write ([]byte (jsonBody ))
321+ }))
322+ defer srv .Close ()
323+
324+ // Point at a .json endpoint to skip the page-scrape branch (HTTP-redirecting
325+ // download.microsoft.com URLs into a test server is more trouble than it's
326+ // worth here). What we're proving is that fetchRawData wraps its inner
327+ // work in GetCached at all — the scrape branch shares the same wrapper.
328+ viper .Set ("azure_endpoint" , srv .URL + "/ServiceTags_Public_20260101.json" )
329+
330+ for i := 0 ; i < 2 ; i ++ {
331+ got , err := fetchRawData (context .Background (), "azure" )
332+ require .NoError (t , err )
333+ assert .Equal (t , jsonBody , got )
334+ }
335+ assert .Equal (t , 1 , hits , "second call should hit cache, not refetch" )
336+ }
337+
338+ func TestFetchRawData_NoCacheBypass (t * testing.T ) {
339+ t .Setenv ("XDG_CACHE_HOME" , t .TempDir ())
340+ t .Cleanup (func () { viper .Reset () })
341+
342+ jsonBody := loadFixture (t )
343+
344+ var hits int
345+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
346+ hits ++
347+ _ , _ = w .Write ([]byte (jsonBody ))
348+ }))
349+ defer srv .Close ()
350+
351+ viper .Set ("azure_endpoint" , srv .URL + "/x.json" )
352+ viper .Set ("no_cache" , true )
353+
354+ for i := 0 ; i < 2 ; i ++ {
355+ _ , err := fetchRawData (context .Background (), "azure" )
356+ require .NoError (t , err )
357+ }
358+ assert .Equal (t , 2 , hits , "--no-cache should refetch every call" )
359+ }
0 commit comments