/*jslint node: true */
/*global describe, it */

const assert  = require('chai').assert;
const request = require('supertest');
const support = require('./support');

describe('GET directory', function() {
  let server = support.makeApp();

  describe('empty directory', function() {
    it('returns an empty directory listing', function(done) {
      request(server)
        .propfind('/remote.php/webdav')
        .set('content-type', 'application/xml')
        .expect(function(res) {
          let listing = support.directoryListing(res);
          assert.equal(listing.length, 1);
          assert.equal(listing[0].href, '/remote.php/webdav/');
        })
        .expect(207, done);
    });
  });

  describe('with a file', function() {
    it('returns directory listing', function(done) {
      request(server)
        .put('/remote.php/webdav/foo.txt')
        .send({foo: 'foobar'})
        .expect(200, function() {
          request(server)
            .propfind('/remote.php/webdav')
            .set('content-type', 'application/xml')
            .expect(function(res) {
              let listing = support.directoryListing(res);
              assert.equal(listing.length, 2);
              assert.equal(listing[1].href, '/remote.php/webdav/foo.txt');
            })
            .expect(207, done);
        });
    });
  });
});

describe('PUT file', function() {
  let server = support.makeApp();

  it('changes the directory etag', function(done) {
    request(server)
      .put('/remote.php/webdav/foo.txt')
      .send({foo: 'foobar'})
      .end(function() {
        request(server)
          .propfind('/remote.php/webdav')
          .end(function(_, res) {
            let listing = support.directoryListing(res);
            let etag = listing[0].etag;
            assert.ok(etag, 'initial directory listing should have etag set');

            setTimeout(function() {
            request(server)
              .put('/remote.php/webdav/bar.txt')
              .send({foo: 'foobar'})
              .expect(200, function() {
                request(server)
                  .propfind('/remote.php/webdav')
                  .set('content-type', 'application/xml')
                  .expect(function(res) {
                    let listing = support.directoryListing(res);
                    let newEtag = listing[0].etag;
                    assert.ok(newEtag, 'new directory listing should have etag set');
                    assert.ok(etag !== newEtag, 'etag should be different than before: ' + etag + ', ' + newEtag);
                  })
                  .expect(207, done);
              });
            }, 1000);
          });
      });
  });
});

@@ -1404,16 +1404,335 @@
 App();%0A%0A
+  it('accepts x-oc-mtime header for owncloud', function(done) %7B%0A    request(server)%0A      .put('/remote.php/webdav/foo.txt')%0A      .send(%7Bfoo: 'foobar'%7D)%0A      .set('x-oc-mtime', '1469294928893')%0A      .end(function(_, res) %7B%0A        assert.equal(res.header%5B'x-oc-mtime'%5D, 'accepted');%0A        done();%0A      %7D);%0A  %7D);%0A%0A
   it('ch

add test for x-oc-mtime header support