<NME> index.test
<BEF> 
// require ('./fixtures/index.test')

const
  { test, read, fetch }
    = require ('snuggsi')

, PORT = 8181
, SCHEME = 'http'
, DOMAIN = 'localhost'
, PATH = 'foo/'
, ROOT = `${SCHEME}://${DOMAIN}:${PORT}/`
, URL  = `${ROOT}${PATH}`


, { Server, Resource }
    = require ('..')

, route = resource =>
    require ('../middleware')
    .route  ( `${PATH}`, resource )

, DEFAULT_METHODS = ['GET', 'HEAD']

, METHODS = [ ... require ('http').METHODS ]
    // cors?
    .filter (method => method !== 'OPTIONS')
    // for some reason connect won't work
    .filter (method => method !== 'CONNECT')
    // Buggs @brandondees?
    .filter (method => method !== 'TRACE')
    // Bind must be called on a function?
    .filter (method => method !== 'BIND')
      == right.slice ().sort ().join `,`


test.only ('Default Resource allowed methods', async t => {
  const
    resource = Resource ``

    methods.indexOf (method) === index

, compare = (left, right) =>
  // headers in no particular order
    left.slice ().sort ().join `,`
      == right.slice ().sort ().join `,`


test `Distribute media to CDN`


test `OPTIONS Resource response`


test `Caching HTML, JSON, Javascript, and CSS`

})


test ('Defined Resource allowed methods', async t => {
  const
    { __proto__: proto } = resource
      = Resource `server/fixtures`
(async assert => {

  const
    resource = Resource ``

  , server =
      (new Server ( [ route (resource) ] ))
      .serve ``

  , ALLOWED_METHODS = DEFAULT_METHODS


  let { status, headers }
    = await fetch ( URL + 'index.html', { method: 'HEAD' } )

  assert ( status == 200 )
  assert.notOk ( headers.get ('allow') )

  server.close ``
})


test `Named Resource (without class) allowed methods`

void (async assert => {
  const
    resource = Resource `/public/`

  , server =
      (new Server ( [ route (resource) ] ))
      .serve ``

  , ALLOWED_METHODS = DEFAULT_METHODS

  for (method of ALLOWED_METHODS) {
    let { status, headers }
      = await fetch ( ROOT + 'index.html', { method } )


    assert ( status == 200 )
    assert.notOk ( headers.get ('allow') )
  }

  server.close ``
})


test `Default Resource allowed methods`

void (async assert => {
  const
    resource = Resource ``

  , server =
      (new Server ( [ route (resource) ] ))
      .serve ``

  , ALLOWED_METHODS = DEFAULT_METHODS

  for (method of ALLOWED_METHODS) {
    let { status, headers }
      = await fetch ( ROOT + 'index.html', { method } )

    assert ( status == 200 )
    assert.notOk ( headers.get ('allow') )
  }

  server.close ``
})


test `Default Resource allowed methods`

void (async assert => {
  const
    resource = Resource ``

  , server =
      (new Server ( [ route (resource) ] ))
      .serve ``

  , ALLOWED_METHODS = DEFAULT_METHODS

  for (method of ALLOWED_METHODS) {
    let { status, headers }
      = await fetch ( URL + 'index.html', { method } )


    assert ( [200, 404 /* for GET SMH */].includes (status) )
    assert.notOk ( headers.get ('allow') )
  }

  server.close ``
})


test `Default Resource disallowed methods`

void (async assert => {
  const
    resource = Resource ``

  , server =
      (new Server ( [ route (resource) ] ))
      .serve ``

  , DISALLOWED_METHODS = METHODS.filter
    (method => !!! DEFAULT_METHODS.includes (method))


  for (method of DISALLOWED_METHODS) {

    let
      { status, headers }
        = await fetch ( URL, { method } )


    let allowed =DEFAULT_METHODS// headers.get ('allow').split `, `

    assert ( status == 405 )
    assert ( compare (allowed, DEFAULT_METHODS) )
  }

  server.close ``
})


test `Defined Resource allowed methods`

void (async assert => {
  const
    { __proto__: proto } = resource
      = Resource `/resource/fixtures/`

  , server =
      (new Server ( [ route (resource) ] ))
      .serve ``

  , DEFINED_METHODS = Object
      .getOwnPropertyNames (Object.getPrototypeOf (proto))
      .map (method => method.toUpperCase ())


  , ALLOWED_METHODS
      = [ ... DEFAULT_METHODS, ... DEFINED_METHODS ]
        .filter (method => METHODS.includes (method))
        .filter (unique)


  for (method of ALLOWED_METHODS) {
    let
      { status, headers }
        = await fetch ( URL, { method } )

    assert ( status != 405 )
    assert.notOk ( headers.get ('allow') )
  }

  server.close ``
})


test `Defined Resource disallowed methods`

void (async assert => {
  const
    { __proto__: proto } = resource
      = Resource `/resource/fixtures/`

  , server =
      (new Server ( [ route (resource) ] ))
      .serve ``

  , DEFINED_METHODS = Object
      .getOwnPropertyNames (Object.getPrototypeOf (proto))
      .map (method => method.toUpperCase ())

  , ALLOWED_METHODS
      = [ ... DEFAULT_METHODS, ... DEFINED_METHODS ]
        .filter (method => METHODS.includes (method))
        .filter (unique)

  , DISALLOWED_METHODS = METHODS
      .filter (name => !!! ALLOWED_METHODS.includes (name))


  for (method of DISALLOWED_METHODS) {

    let
      { status, headers }
        = await fetch ( URL, { method } )

    let allowed =DEFAULT_METHODS// headers.get ('allow').split `, `

    assert ( status == 405 )
    assert ( compare (allowed, ALLOWED_METHODS) )
  }

  server.close ``
})


test `GET subresource assets`

void (async assert => {

  const
    point = `/resource/fixtures/`
  , resource = Resource (point)

  , assets // loop resource
      = ['index.html']

  , server =
      (new Server ( [ route (resource) ] ))
      .serve ``

  for (asset of assets) {

    let
      expected = await read
        ( `.${point}${asset}` )

    , response = await fetch
        ( `${URL}${asset}` )

    , { status, headers } = response

    , type     = headers.get ('Content-Type')
    , length   = headers.get ('Content-Length')
    , modified = headers.get ('Last-Modified')


    assert ( expected == await response.text `` )
    assert ( type == 'text/html; charset=utf-8' )
    assert ( length == expected.length )
    assert ( status == 200 )
    assert ( modified )
  }

  server.close ``
})


test `Deny GET */index.es subresource`

void (async assert => {

  const
    point = `/resource/fixtures/`
  , resource = Resource (point)

  , server =
      (new Server ( [ route (resource) ] ))
      .serve ``

  , response = await fetch
      ( `${URL}index.es` )

  , { status } = response


  assert ( status == 404 )


  server.close ``
})


test `GET subresource with predefined body`

void (async assert => {

  const
    resource = Resource `/resource/fixtures/`

  , server =
      (new Server ( [ route (resource) ] ))
      .serve ``

  , response = await fetch
      ( `${URL}ping` )

  , { status, headers } = response


  assert ( status == 200 )
  assert ( 'pong' == await response.text `` )

  server.close ``
})

<MSG> Fix whitespace and update only test

<DFF> @@ -34,7 +34,7 @@ const
       == right.slice ().sort ().join `,`
 
 
-test.only ('Default Resource allowed methods', async t => {
+test ('Default Resource allowed methods', async t => {
   const
     resource = Resource ``
 
@@ -57,7 +57,7 @@ test.only ('Default Resource allowed methods', async t => {
 })
 
 
-test ('Defined Resource allowed methods', async t => {
+test.only ('Defined Resource allowed methods', async t => {
   const
     { __proto__: proto } = resource
       = Resource `server/fixtures`

{'commit': "diff --git a/resource/index.es b/resource/index.es\nindex 176f2a16..9f516e0d 100644\n--- a/resource/index.es\n+++ b/resource/index.es\n@@ -7,7 +7,7 @@ const\n       .filter (method => method !== 'OPTIONS') // cors?\n \n , DEFAULT_METHODS\n-    = ['GET', 'HEAD']\n+    = [ 'GET', 'HEAD' ]\n \n , SAFE_METHODS\n     = [ ... DEFAULT_METHODS ]\ndiff --git a/resource/index.test b/resource/index.test\nindex 5d2ca31e..0a55307b 100644\n--- a/resource/index.test\n+++ b/resource/index.test\n@@ -34,7 +34,7 @@ const\n       == right.slice ().sort ().join `,`\n \n \n-test.only ('Default Resource allowed methods', async t => {\n+test ('Default Resource allowed methods', async t => {\n   const\n     resource = Resource ``\n \n@@ -57,7 +57,7 @@ test.only ('Default Resource allowed methods', async t => {\n })\n \n \n-test ('Defined Resource allowed methods', async t => {\n+test.only ('Defined Resource allowed methods', async t => {\n   const\n     { __proto__: proto } = resource\n       = Resource `server/fixtures`\n", 'commit_subject': 'Fix whitespace and update only test', 'repo_name': 'devpunks/snuggsi', 'committer': {'name': 'Snuggs', 'email': 'c6b737713e2e92b7bd225b33e0014d7aadc48be6@gmail.com', 'time_sec': '1528734970', 'tz_offset': '0', 'date': {'seconds': '1528734970'}}, 'commit_tree': '1f4f09fd38bcc5e2864d6eddffaeb7b20d14a226', 'commit_message': 'Fix whitespace and update only test\n', 'language_name': 'HTML', 'license': 'mit', 'hunks': "@@ -34,7 +34,7 @@ const\n       == right.slice ().sort ().join `,`\n \n \n-test.only ('Default Resource allowed methods', async t => {\n+test ('Default Resource allowed methods', async t => {\n   const\n     resource = Resource ``\n \n@@ -57,7 +57,7 @@ test.only ('Default Resource allowed methods', async t => {\n })\n \n \n-test ('Defined Resource allowed methods', async t => {\n+test.only ('Defined Resource allowed methods', async t => {\n   const\n     { __proto__: proto } = resource\n       = Resource `server/fixtures`\n", 'addition_count': 2, 'deletion_count': 2, 'src_file': 'a/resource/index.test', 'tgt_file': 'b/resource/index.test', 'file_extension': '.test', 'before_file': "\n// require ('./fixtures/index.test')\n\nconst\n  { test, read, fetch }\n    = require ('snuggsi')\n\n, PORT = 8181\n, SCHEME = 'http'\n, DOMAIN = 'localhost'\n, PATH = 'foo/'\n, ROOT = `${SCHEME}://${DOMAIN}:${PORT}/`\n, URL  = `${ROOT}${PATH}`\n\n\n, { Server, Resource }\n    = require ('..')\n\n, route = resource =>\n    require ('../middleware')\n    .route  ( `${PATH}`, resource )\n\n, DEFAULT_METHODS = ['GET', 'HEAD']\n\n, METHODS = [ ... require ('http').METHODS ]\n    // cors?\n    .filter (method => method !== 'OPTIONS')\n    // for some reason connect won't work\n    .filter (method => method !== 'CONNECT')\n    // Buggs @brandondees?\n    .filter (method => method !== 'TRACE')\n    // Bind must be called on a function?\n    .filter (method => method !== 'BIND')\n      == right.slice ().sort ().join `,`\n\n\ntest.only ('Default Resource allowed methods', async t => {\n  const\n    resource = Resource ``\n\n    methods.indexOf (method) === index\n\n, compare = (left, right) =>\n  // headers in no particular order\n    left.slice ().sort ().join `,`\n      == right.slice ().sort ().join `,`\n\n\ntest `Distribute media to CDN`\n\n\ntest `OPTIONS Resource response`\n\n\ntest `Caching HTML, JSON, Javascript, and CSS`\n\n})\n\n\ntest ('Defined Resource allowed methods', async t => {\n  const\n    { __proto__: proto } = resource\n      = Resource `server/fixtures`\n(async assert => {\n\n  const\n    resource = Resource ``\n\n  , server =\n      (new Server ( [ route (resource) ] ))\n      .serve ``\n\n  , ALLOWED_METHODS = DEFAULT_METHODS\n\n\n  let { status, headers }\n    = await fetch ( URL + 'index.html', { method: 'HEAD' } )\n\n  assert ( status == 200 )\n  assert.notOk ( headers.get ('allow') )\n\n  server.close ``\n})\n\n\ntest `Named Resource (without class) allowed methods`\n\nvoid (async assert => {\n  const\n    resource = Resource `/public/`\n\n  , server =\n      (new Server ( [ route (resource) ] ))\n      .serve ``\n\n  , ALLOWED_METHODS = DEFAULT_METHODS\n\n  for (method of ALLOWED_METHODS) {\n    let { status, headers }\n      = await fetch ( ROOT + 'index.html', { method } )\n\n\n    assert ( status == 200 )\n    assert.notOk ( headers.get ('allow') )\n  }\n\n  server.close ``\n})\n\n\ntest `Default Resource allowed methods`\n\nvoid (async assert => {\n  const\n    resource = Resource ``\n\n  , server =\n      (new Server ( [ route (resource) ] ))\n      .serve ``\n\n  , ALLOWED_METHODS = DEFAULT_METHODS\n\n  for (method of ALLOWED_METHODS) {\n    let { status, headers }\n      = await fetch ( ROOT + 'index.html', { method } )\n\n    assert ( status == 200 )\n    assert.notOk ( headers.get ('allow') )\n  }\n\n  server.close ``\n})\n\n\ntest `Default Resource allowed methods`\n\nvoid (async assert => {\n  const\n    resource = Resource ``\n\n  , server =\n      (new Server ( [ route (resource) ] ))\n      .serve ``\n\n  , ALLOWED_METHODS = DEFAULT_METHODS\n\n  for (method of ALLOWED_METHODS) {\n    let { status, headers }\n      = await fetch ( URL + 'index.html', { method } )\n\n\n    assert ( [200, 404 /* for GET SMH */].includes (status) )\n    assert.notOk ( headers.get ('allow') )\n  }\n\n  server.close ``\n})\n\n\ntest `Default Resource disallowed methods`\n\nvoid (async assert => {\n  const\n    resource = Resource ``\n\n  , server =\n      (new Server ( [ route (resource) ] ))\n      .serve ``\n\n  , DISALLOWED_METHODS = METHODS.filter\n    (method => !!! DEFAULT_METHODS.includes (method))\n\n\n  for (method of DISALLOWED_METHODS) {\n\n    let\n      { status, headers }\n        = await fetch ( URL, { method } )\n\n\n    let allowed =DEFAULT_METHODS// headers.get ('allow').split `, `\n\n    assert ( status == 405 )\n    assert ( compare (allowed, DEFAULT_METHODS) )\n  }\n\n  server.close ``\n})\n\n\ntest `Defined Resource allowed methods`\n\nvoid (async assert => {\n  const\n    { __proto__: proto } = resource\n      = Resource `/resource/fixtures/`\n\n  , server =\n      (new Server ( [ route (resource) ] ))\n      .serve ``\n\n  , DEFINED_METHODS = Object\n      .getOwnPropertyNames (Object.getPrototypeOf (proto))\n      .map (method => method.toUpperCase ())\n\n\n  , ALLOWED_METHODS\n      = [ ... DEFAULT_METHODS, ... DEFINED_METHODS ]\n        .filter (method => METHODS.includes (method))\n        .filter (unique)\n\n\n  for (method of ALLOWED_METHODS) {\n    let\n      { status, headers }\n        = await fetch ( URL, { method } )\n\n    assert ( status != 405 )\n    assert.notOk ( headers.get ('allow') )\n  }\n\n  server.close ``\n})\n\n\ntest `Defined Resource disallowed methods`\n\nvoid (async assert => {\n  const\n    { __proto__: proto } = resource\n      = Resource `/resource/fixtures/`\n\n  , server =\n      (new Server ( [ route (resource) ] ))\n      .serve ``\n\n  , DEFINED_METHODS = Object\n      .getOwnPropertyNames (Object.getPrototypeOf (proto))\n      .map (method => method.toUpperCase ())\n\n  , ALLOWED_METHODS\n      = [ ... DEFAULT_METHODS, ... DEFINED_METHODS ]\n        .filter (method => METHODS.includes (method))\n        .filter (unique)\n\n  , DISALLOWED_METHODS = METHODS\n      .filter (name => !!! ALLOWED_METHODS.includes (name))\n\n\n  for (method of DISALLOWED_METHODS) {\n\n    let\n      { status, headers }\n        = await fetch ( URL, { method } )\n\n    let allowed =DEFAULT_METHODS// headers.get ('allow').split `, `\n\n    assert ( status == 405 )\n    assert ( compare (allowed, ALLOWED_METHODS) )\n  }\n\n  server.close ``\n})\n\n\ntest `GET subresource assets`\n\nvoid (async assert => {\n\n  const\n    point = `/resource/fixtures/`\n  , resource = Resource (point)\n\n  , assets // loop resource\n      = ['index.html']\n\n  , server =\n      (new Server ( [ route (resource) ] ))\n      .serve ``\n\n  for (asset of assets) {\n\n    let\n      expected = await read\n        ( `.${point}${asset}` )\n\n    , response = await fetch\n        ( `${URL}${asset}` )\n\n    , { status, headers } = response\n\n    , type     = headers.get ('Content-Type')\n    , length   = headers.get ('Content-Length')\n    , modified = headers.get ('Last-Modified')\n\n\n    assert ( expected == await response.text `` )\n    assert ( type == 'text/html; charset=utf-8' )\n    assert ( length == expected.length )\n    assert ( status == 200 )\n    assert ( modified )\n  }\n\n  server.close ``\n})\n\n\ntest `Deny GET */index.es subresource`\n\nvoid (async assert => {\n\n  const\n    point = `/resource/fixtures/`\n  , resource = Resource (point)\n\n  , server =\n      (new Server ( [ route (resource) ] ))\n      .serve ``\n\n  , response = await fetch\n      ( `${URL}index.es` )\n\n  , { status } = response\n\n\n  assert ( status == 404 )\n\n\n  server.close ``\n})\n\n\ntest `GET subresource with predefined body`\n\nvoid (async assert => {\n\n  const\n    resource = Resource `/resource/fixtures/`\n\n  , server =\n      (new Server ( [ route (resource) ] ))\n      .serve ``\n\n  , response = await fetch\n      ( `${URL}ping` )\n\n  , { status, headers } = response\n\n\n  assert ( status == 200 )\n  assert ( 'pong' == await response.text `` )\n\n  server.close ``\n})\n", 'message': 'Fix whitespace and update only test\n', 'author_name': 'Snuggs', 'lang': 'test'}